如何在图像量化后读取和存储每个像素的rgb值

时间:2011-02-22 02:27:12

标签: matlab

您好 我用图像将图像量化为64种颜色 [IMG1,地图] = rgb2ind(RGB_img,64); 现在我想读取量化图像'img1'的每个像素的rbg值并将其存储在变量中。 请帮忙 带着敬意 dev的

3 个答案:

答案 0 :(得分:1)

如果这样可行则不正确,但这是matlab文档中的内容

色彩图是一个m乘3的实数矩阵,介于0.0和1.0之间。每行是定义一种颜色的RGB矢量。色图的第k行定义第k个颜色,其中map(k,:) = [r(k)g(k)b(k)])指定红色,绿色和蓝色的强度。

所以 map(k,1)* 255可以得到第k个颜色在0-255范围内的R值。您需要实现一些逻辑来确定每个像素的索引并将其映射到相应的值。

抱歉,我现在没有使用matlab,或者我会尝试一下。

答案 1 :(得分:1)

您可以通过使用索引图像值索引64 x 3彩色地图的每个相应列来获取量化图像的红色,绿色和蓝色分量:

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

请注意,将img1用作索引时必须加1,因为索引值从0到63运行。

例如,以下是'peppers.png'使用imageFile的一些结果:

enter image description here

答案 2 :(得分:0)

这是我一段时间以来为自己的娱乐所做的事情。 跳过主要功能。 在函数中查找“ofstream out”对象。希望你能得到一些想法。 我认为这段代码充满了错误。

#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;
}