Reputation: 21
Hi i have quantized an image to 64 colors using [img1,map]=rgb2ind(RGB_img,64); now i want to read the rbg values of each pixel of the quantized image 'img1' and store it in a variable. please help with regards dev
Upvotes: 2
Views: 10433
Reputation: 125874
You can get the red, green, and blue components of the quantized image by indexing each corresponding column of the 64-by-3 color map with the indexed image values:
RGB_img = imread(imageFile); %# Load an RGB image
[img1,map] = rgb2ind(RGB_img,64); %# Create your quantized image
rPlane = reshape(map(img1+1,1),size(img1)); %# Red color plane for image
gPlane = reshape(map(img1+1,2),size(img1)); %# Green color plane for image
bPlane = reshape(map(img1+1,3),size(img1)); %# Blue color plane for image
Notice you have to add 1 to img1
when using it as an index since the index values run from 0 to 63.
For example, here are some results using 'peppers.png'
for imageFile
:
Upvotes: 1
Reputation: 7008
here's something I did for my own amusement a while back. Skip the main function. Look for the "ofstream out" object in the functions. Hope u will get some idea. This code is full of bugs I think.
#include <iostream.h>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <sys/stat.h>
#include <math.h>
#include <windows.h>
using namespace std;
void Encrypt();
void Decrypt();
int main(int argc, char* argv[])
{
while(true){
system("cls");
cout << "1. Encrypt" << endl;
cout << "2. Decrypt" << endl;
cout << "3. Exit" << endl;
int choice;
cout << endl << "Enter choice : " ;
cin >> choice;
if(choice == 1)
Encrypt();
else if(choice == 2)
Decrypt();
else
exit(0);
}
return 0;
}
void Encrypt()
{
ifstream in_image;
ifstream in_file;
ofstream out;
system("cls");
char* im_in = new char[200];
char* f_in = new char[200];
char* im_out = new char[200];
cout << "Enter input image file(.tga) : " ;
cin >> im_in;
cout << "Enter input file : " ;
cin >> f_in;
cout << "Enter output file : " ;
cin >> im_out;
in_image.open(im_in, ios::in|ios::binary);
in_file.open(f_in, ios::in|ios::binary);
out.open(im_out, ios::out|ios::binary);
// struct stat results;
// if(stat(f_in, &results) != 0)
// {
// cout << "Error opening input file!" << endl;
// exit(1);
// }
delete im_in;
delete im_out;
delete f_in;
int i, j, k;
char* image_header = new char[54];
char* image_buffer = new char[16];
char* file_in_buffer = new char[1];
char temp;
int *bits = new int[8]; //Array to keep the bits in a byte.
int operand;
in_image.read(image_header, 54);
int f_length = 0;
image_header[5] = (char)f_length%256;
image_header[6] = (char)(f_length/256)%256;
image_header[7] = (char)(f_length/256)/256;
out.write(image_header, 54);
i = 0;
do
{
in_image.read(image_buffer, 16);
in_file.read(file_in_buffer, 1);
if(in_file)
{
operand = 1;
for(j = 0; j <8; j++)
{
if((file_in_buffer[0] & operand) != 0) //Copying the file_in_buffer to the array.
bits[j] = 1;
else
bits[j] = 0;
operand = operand << 1;
}
k=0;
for(j = 0; j < 16; j++)
{
if(j%4 == 0 || j%4 == 1)
{
image_buffer[j] = image_buffer[j] & (-2); //Making the last bit 0.
image_buffer[j] = image_buffer[j] ^ bits[k];
k++;
}
}
}
out.write(image_buffer, 16);
i++;
}while(in_image);
in_image.close();
in_file.close();
out.close();
delete image_header;
delete image_buffer;
delete file_in_buffer;
delete bits;
}
void Decrypt()
{
system("cls");
ifstream image;
ofstream out;
char* im_in = new char[200];
char* f_out = new char[200];
cout << "Enter input image file(.tga) : " ;
cin >> im_in;
cout << "Enter output file : " ;
cin >> f_out;
image.open(im_in, ios::in | ios::binary);
out.open(f_out, ios::out|ios::binary);
delete im_in;
delete f_out;
char image_header[54];
char image_buffer[16];
char* temp = new char[1];
int tempi;
int bits[8];
int i, j, k;
int operand;
image.read(image_header, 54);
int f_length = 0;
for(i = 0; i <1; i ++)
{
if(image_header[i+5] > -1)
f_length += image_header[i+5]*pow(256, i);
else
f_length += (256 + image_header[i+5])*pow(256, i);
}
i = 0;
for(i = 0; i < f_length; i++)
{
for(j = 0; j < 8; j++)
{
bits[j] = 0;
}
image.read(image_buffer, 16);
k = 0;
for(j = 0; j <16; j++)
{
if(j%4 == 0 || j%4 == 1)
{
bits[k] = image_buffer[j] & 1;
k++;
}
}
tempi = 0;
operand = 1;
for(j = 0; j < 8; j++)
{
tempi += (bits[j]<<j) & operand;
operand = operand << 1;
}
temp[0] = (char)tempi;
out.write(temp, 1);
}while(image);
image.close();
out.close();
delete image_header;
delete image_buffer;
// delete temp;
}
Upvotes: 0
Reputation: 1025
Not positive if this will work, but this is what is from the matlab documentation
A colormap is an m-by-3 matrix of real numbers between 0.0 and 1.0. Each row is an RGB vector that defines one color. The kth row of the colormap defines the kth color, where map(k,:) = [r(k) g(k) b(k)]) specifies the intensity of red, green, and blue.
so map(k,1)*255 would get you the R value in the range of 0-255 for the kth color. You would need to implement some logic to determine the index of each pixel and map it into the corresponding value.
sorry I don't have matlab on the box I am on right now or I would give it a try.
Upvotes: 1