Mostafiz
Mostafiz

Reputation: 1687

copying ipl image pixel by pixel

The problem is solved....I used cvGet2D,below is the sample code

        CvScalar s;
        s=cvGet2D(src_Image,pixel[i].x,pixel[i].y);         
        cvSet2D(dst_Image,pixel[i].x,pixel[i].y,s);

Where src_Iamge and dst_Image is the source and destination image correspondingly and pixel[i] is the selected pixel i wanted to draw in the dst image. I have include the real out image below.

have an source Ipl image, I want to copy some of the part of the image to a new destination image pixel by pixel. can any body tell me how can do it? I use c,c++ in opencv. For example if the below image is source image, enter image description here

The real output imageenter image description here

Upvotes: 0

Views: 2237

Answers (2)

Barney Szabolcs
Barney Szabolcs

Reputation: 12514

EDIT:

I can see the comments suggesting cvGet2d. I think, if you just want to show "points", it is best to show them with a small neighbourhood so they can be seen where they are. For that you can draw white filled circles with origins at (x,y), on a mask, then you do the copyTo.

using namespace cv;

Mat m(input_iplimage);
Mat mask=Mat::zeros(m.size(), CV_8UC1);

p1 = Point(x,y); 
r = 3;
circle(mask,p1,r, 1); // draws the circle around your point.
floodFill(mask, p1, 1); // fills the circle.

//p2, p3, ...

Mat output = Mat::zeros(m.size(),m.type()); // output starts with a black background.
m.copyTo(output, mask); // copies the selected parts of m to output     

OLD post:

Create a mask and copy those pixels:

#include<opencv2/opencv.hpp>
using namespace cv;

Mat m(input_iplimage);
Mat mask=Mat::zeros(m.size(), CV_8UC1); // set mask 1 for every pixel you wanna copy.
Rect roi=Rect(x,y,width,height);  // create a rectangle
mask(roi) = 1;   // set it to 0.
roi = Rect(x2,y2,w2,h2);
mask(roi)=1;     // set the second rectangular area for copying...

Mat output = 100*Mat::ones(m.size(),m.type()); // output with a gray background.
m.copyTo(output, mask); // copy selected areas of m to output

Alternatively you can copy Rect-by-Rect:

Mat m(input_iplimage);
Mat output = 100*Mat::ones(m.size(),m.type()); // output with a gray background.

Rect roi=Rect(x,y,width,height);
Mat m_temp, out_temp;
m_temp=m(roi);
out_temp = output(roi);
m_temp.copyTo(out_temp);

roi=Rect(x2,y2,w2,h2);
Mat m_temp, out_temp;
m_temp=m(roi);
out_temp = output(roi);
m_temp.copyTo(out_temp);

Upvotes: 1

gui
gui

Reputation: 425

The answer to your question only requires to have look at the OpenCV documentation or just to search in your favourite search engine.

Here you've an answer for Ipl images and for newer Mat data.

For having an output as I see in your images, I'd do it setting ROI's, it's more efficient.

Upvotes: 0

Related Questions