Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

working with pixel in Mat

i run the following program for copying image pixel by pixel

    #include"opencv2/imgproc/imgproc.hpp"
#include"opencv2/core/core.hpp"
#include"opencv2/highgui/highgui.hpp"
#include<iostream>

using namespace cv;
using namespace std;

int main (int argc, char** argv)
{
 if (argc!=2)
{
 cout <<"Usage: specify the image"<<endl;
return -1;
}
Mat img=imread(argv[1],CV_LOAD_IMAGE_COLOR);
Mat A(img.size(),img.type());
if(!img.data)
{
cout<<"could not open or find the image"<<endl;
return -1;
}

namedWindow("Display window",0);
//namedWindow("gray",0);
namedWindow("threshold",0);
imshow("Display window",A);

for (int i=0;i<img.rows;i++)
{
for(int j=0;j<img.cols;j++)
{
Vec3f intensity = img.at<Vec3f>(i,j);
float blue=intensity.val[0];
float green=intensity.val[1];
float red=intensity.val[2];

A.data[A.channels()*(A.cols*i + j) + 0] = blue;
A.data[A.channels()*(A.cols*i + j) + 1] = green;
A.data[A.channels()*(A.cols*i + j) + 2] = red;
    }
}

//cout<<red<<","<<blue<<","<<green<<endl;
imwrite("etc1.png",A);
imshow("threshold",A);
waitKey(0);

}

input image

image description

output image

image description

where is the mistake?