First time here? Check out the FAQ!

Ask Your Question
0

copy half image

asked Jan 29 '18

julian403 gravatar image

updated Jan 29 '18

Hello All.

I have an image:

MAt gray which the size is 480*640 and its 1 channel and I need to copy the half images:

Mat edges1 (480, 640/2, CV_8U); Mat edges2 (480,640/2, CV_8U);

for(int i=0;i<480;i++) { for(int j=0;j<640/2;j++) {

edges1.at<uchar>(i,j)=gray.at<uchar>(i,j);

} }

for(int i=0;i<480;i++) { for(int j=640/2;j<640;j++) {

edges2.at<uchar>(i,j)=gray.at<uchar>(i,j);

} } imwrite("bordes1.png", edges1,compression_params); imwrite("bordes2.png", edges2,compression_params); return 1; }

But I got Segmentation fault. What can it be?

Preview: (hide)

Comments

you can find proper code sample here

sturkmen gravatar imagesturkmen (Jan 29 '18)edit

Oh, thanks a lot. But whats the problem with my code? I'm seeing that work in a low leves with opencv always get problem. I have to use the function defines in opencv headres to make samething.

julian403 gravatar imagejulian403 (Jan 30 '18)edit

i did not try your code but try swapping i & j ( this code works slow)

edges2.at<uchar>(j,i)=gray.at<uchar>(j,i);
sturkmen gravatar imagesturkmen (Jan 30 '18)edit

your 2nd case starts counting j at 640/2, which is correct for the src image, but already out of bounds for the dst image.

berak gravatar imageberak (Jan 30 '18)edit

you are right! thanks

julian403 gravatar imagejulian403 (Jan 30 '18)edit

1 answer

Sort by » oldest newest most voted
0

answered Jan 30 '18

berak gravatar image

again please do not write loops for this. cv::Mat has a roi operator, allowing you to specify a subrect in a single line (also, without having to copy any data !):

Mat edges = ....
imwrite("left.png",  edges(Rect(0,0,           edges.cols/2,edges.rows)));
imwrite("right.png", edges(Rect(edges.cols/2,0,edges.cols/2,edges.rows)));
Preview: (hide)

Question Tools

1 follower

Stats

Asked: Jan 29 '18

Seen: 718 times

Last updated: Jan 30 '18