0

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

David Duponchel
  • 3,959
  • 3
  • 28
  • 36
MMH
  • 1,676
  • 5
  • 26
  • 43
  • Well, you could do it just as you described, I mean an IplImage has a pointer to the data, and you could just manually implement something to copy a ROI. – SinisterMJ Nov 22 '12 at 08:39
  • I don't want to set ROI i want to copy some of the pixels and show it in the output image. – MMH Nov 22 '12 at 08:58
  • 1
    You can use [`cvGet2D`](http://docs.opencv.org/modules/core/doc/old_basic_structures.html?highlight=cvget2d#get-d) – sgarizvi Nov 22 '12 at 10:15
  • Thank Sgar..I am also thinking of using cvGet2D..I will let you know if it works... – MMH Nov 23 '12 at 01:50

2 Answers2

1

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);
Barney Szabolcs
  • 11,846
  • 12
  • 66
  • 91
  • thanks for your answer. I don't want to do ROI as it takes rect wise. suppose From the source image I know information about 20 pixels (x,y position information) I want to show this pixels in their real shape(may be polygon but not rectangle). But if I use ROI i will have to show a rectangle. – MMH Nov 23 '12 at 01:32
  • well, I have shown you how you can assemble masks, too. (see "create a mask") And it is up to you what shape you draw in that. (If you draw a white filled circle into the mask, you will see circles...) – Barney Szabolcs Nov 23 '12 at 02:04
0

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.

Community
  • 1
  • 1
gui
  • 425
  • 4
  • 18
  • thanks for your opinion..but I don't want to use ROI.Suppose I know info about only 10 pixels and they are in round or some other shape..I want to show their real shape.If I use ROI then I have to show as a rectangle. – MMH Nov 23 '12 at 01:27
  • well, in the answer that I posted you can find a code to copy pixel per pixel that allows you to do that. – gui Nov 23 '12 at 09:04