Ask Your Question
0

copy half image

asked 2018-01-29 16:57:48 -0600

julian403 gravatar image

updated 2018-01-29 17:00:02 -0600

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?

edit retag flag offensive close merge delete

Comments

you can find proper code sample here

sturkmen gravatar imagesturkmen ( 2018-01-29 17:11:51 -0600 )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 ( 2018-01-29 18:42:35 -0600 )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 ( 2018-01-29 18:53:50 -0600 )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 ( 2018-01-30 03:22:55 -0600 )edit

you are right! thanks

julian403 gravatar imagejulian403 ( 2018-01-30 07:43:12 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-01-30 03:21:08 -0600

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)));
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-01-29 16:57:48 -0600

Seen: 711 times

Last updated: Jan 30 '18