Access sub-image in opencv

asked 2016-10-12 03:13:17 -0600

mosta gravatar image

Hi,

I am new in opencv, i am using opencv 3.0, i am trying to access a sub-image,

Original image

As i know there is a 2 ways to do it, using rect or range

using range to access the top quarter of the image

using namespace cv;
int main()
{
   Mat img_l = imread("left2.png",IMREAD_GRAYSCALE);
   img_l.convertTo(img_l, CV_64FC1); //convert to double in gray scale
   Mat leftROI = img_l(Range(0, img_l.rows/2), Range(0,img_l.cols/2));
   cv::normalize(leftROI, leftROI, 0, 1, cv::NORM_MINMAX);  //to view
   imshow("opencvtest1",leftROI);
   waitKey(0);
   return 0;
 }

The output is

using rect

using namespace cv;
int main()
{
   Mat img_l = imread("left2.png",IMREAD_GRAYSCALE);
   img_l.convertTo(img_l, CV_64FC1); //convert to double in gray scale
   Mat leftROI = img_l(Rect(0,0, img_l.cols/2 , img_l.rows/2));
   cv::normalize(leftROI, leftROI, 0, 1, cv::NORM_MINMAX);  //to view
   imshow("opencvtest1",leftROI);
   waitKey(0);
   return 0;
 }

The output is

However the output images does not seem right, the sub images are no the top left quarter
but when i use matlab

x = imread('left2.png' );
x = rgb2gray(x);
leftROI = x(1:size(x,1)/2,1:size(x,2)/2);
imshow(leftROI)

The output is ,

The output of matlab seems right

could anyone help why is opencv not right?

edit retag flag offensive close merge delete

Comments

1

can you try to save the roi images to disc (imwrite), to rule out, that it's a gui/windowing problem ?

(can't reproduce your prob on opencv2.4 or 3.1, win/linux)

berak gravatar imageberak ( 2016-10-12 03:34:58 -0600 )edit

It is written correctly, thanks !

mosta gravatar imagemosta ( 2016-10-12 11:42:57 -0600 )edit