Ask Your Question

how do I copy specific pixels to destination mat from source mat in opencv c++?

asked 2017-11-23 01:33:35 -0600

Explorer_DS gravatar image

updated 2017-11-23 05:40:18 -0600

I want to copy specific pixels to destination mat when certain condition becomes addition to that I a switching to next rows upon specific count of pixels that met condition. But it throws memory related exception as follows:

** Error in `/home/ds/eclipse-workspace/pixelenc_current/Debug/': malloc(): memory corruption (fast): 0x0000000001593710 ***
======= Backtrace: =========

platform :centos7 +opencv 3.3


unsigned char *input = (unsigned char*)(;  //mask
unsigned char *imginput = (unsigned char*)(; // source data
int m=0,n=0;
if(hfcnt>=htotalpix)  // max no of pixels 
int cnt=0;
valmask=input[mask.cols *i  +j ] ;
if(valmask==255)  /*condition*/
{<Vec3b>(m,n)[0]=imginput[j*img.cols*1 + i*1 + 0];<Vec3b>(m,n)[1]=imginput[j*img.cols*2 + i*2 + 0];<Vec3b>(m,n)[2]=imginput[j*img.cols*2 + i*3 + 0];
/* to switch to next row in source*/

here are the images extracted using copyTo , in these images there is a area having black pixels which I don't want to copy. That's why I am copying specific pixels.

image description image description

Here is one of the mask I used to extract sub images. Its different one, right now I cannot produce the mask used to extract above images. image description

edit retag flag offensive close merge delete



what is hinfo and patch1 ?

berak gravatar imageberak ( 2017-11-23 01:58:30 -0600 )edit

your code is broken beyond repair. let's simply forget that.

please try to be more explicit on what you're trying to achieve, so we can help you find a high-level solution for it.

again, writing for-loops like above is always the wrong idea, and you're the umpteeth person shooting his foot like that

berak gravatar imageberak ( 2017-11-23 02:13:36 -0600 )edit

Exception is in name cvNamedWindow. Where is cvNamedWindow in your code?

Why do you write :<Vec3b>(m,n)[0]=imginput[j*img.cols*1 + i*1 + 0];<Vec3b>(m,n)[0]=imginput[j*img.cols*2 + i*2 + 0];<Vec3b>(m,n)[0]=imginput[j*img.cols*2 + i*3 + 0];

last line is enough

You use mask.cols i I understand i is a row index... and imginput[jimg.cols*1 now j is a row index ?

LBerger gravatar imageLBerger ( 2017-11-23 02:13:53 -0600 )edit

^^ imshow() calls namedWindow(), which calls cvNamedWindow()

berak gravatar imageberak ( 2017-11-23 02:26:08 -0600 )edit

I want to copy specific pixels from 3 channel matrix to another matrix. Path1 is also 3 channel matrix of same type as of img. its 0,1,2 in<vec3b>(m,n)[0].... valmask used in condition checking to check whether that specific pixel is a part of mask or not hinfo[] contains lower and upper bounds of for loop

once copy is completed I am trying to disply patch1 as follows; namedWindow("patch1", CV_WINDOW_AUTOSIZE ); imshow( "patch1", patch1);

Explorer_DS gravatar imageExplorer_DS ( 2017-11-23 02:31:35 -0600 )edit

I am trying to copy only masked pixels to another patch of same type as of source matrix which is img.

Explorer_DS gravatar imageExplorer_DS ( 2017-11-23 02:34:08 -0600 )edit

I tried with following code too, but same exception<vec3b>(m,n)<vec3b>(i.j);

Explorer_DS gravatar imageExplorer_DS ( 2017-11-23 02:40:32 -0600 )edit

please do not try to "repair" this code.

berak gravatar imageberak ( 2017-11-23 03:10:56 -0600 )edit

instead of understanding the problem and getting to the solution people are down voting the question

Explorer_DS gravatar imageExplorer_DS ( 2017-11-28 06:18:57 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-11-23 02:40:25 -0600

berak gravatar image

updated 2017-11-23 05:25:14 -0600

so the answer is: use copyTo() with a mask (and a roi):

Rect roi = // something based on hinfo
img(roi).copyTo(patch1, mask);

please try to use opencv's builtin functions, not to defeat it writing ill fated for-loops, like above !

edit flag offensive delete link more


it will copy whole square or rectangular shaped ROI , What I want to copy is specific region masked within that ROI.

Explorer_DS gravatar imageExplorer_DS ( 2017-11-23 03:01:40 -0600 )edit

that's what the mask should be for, no ?

berak gravatar imageberak ( 2017-11-23 03:09:50 -0600 )edit

copyTO will copy entire x +w , y+height that contains min_x, min_y, max_x, max_y coordinates of mask region. my mask is in polygonal shape so I just want to copy pixels that are part of polygon.

Explorer_DS gravatar imageExplorer_DS ( 2017-11-23 05:02:33 -0600 )edit

you need a (black)mask in the same size of the roi, you want to copy, and that should contain a (filled ?) white polygon

berak gravatar imageberak ( 2017-11-23 05:16:38 -0600 )edit

I already did that, in fact above images are extracted using polygonal mask that has white portion which marks area to be copied. Now what I want is to copy some pixels from these images to new matrix one by one.

Explorer_DS gravatar imageExplorer_DS ( 2017-11-23 05:24:42 -0600 )edit

show your mask, then.

berak gravatar imageberak ( 2017-11-23 05:25:42 -0600 )edit

After copyTo I have cropped images to avoid extra processing

Explorer_DS gravatar imageExplorer_DS ( 2017-11-23 05:43:41 -0600 )edit

Question Tools

1 follower


Asked: 2017-11-23 01:33:35 -0600

Seen: 2,550 times

Last updated: Nov 28 '17