Ask Your Question
0

Display 4 images and save as in a table (opencv , C++ )

asked 2014-06-20 02:56:42 -0600

abir gravatar image

updated 2014-06-20 03:18:35 -0600

Hello,

The code will read an image and cut into 4 parts so 4 small images. My problem is to display 4 images and save in a table.

I can display only the last image but not all with imshow : imshow ( "smallImages", cv::Mat ( image , rect ));

Please help me and think you.

for  ( int y =  0 ; y < image . rows ; y += smallSize . height )
    {
        for  ( int x =  0 ; x < image  . cols ; x += smallSize . width )
        {
            cv :: Rect rect =   cv :: Rect ( x , y , smallSize . width , smallSize . height );
            smallImages . push_back ( cv :: Mat ( image  , rect ));
            imshow ( "smallImages", cv::Mat ( image , rect ));

        }
    }
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2014-06-20 03:36:14 -0600

updated 2014-06-25 03:06:02 -0600

You should use a different name for each image:

char image_name[ 4 ][ 255 ] = { "image1", "image2", "image3", "image4" };
for( int y = 0, i = 0 ; y < image.rows ; y += smallSize.height )
    for( int x = 0 ; x < image.cols ; x += smallSize.width, ++i )
    {
        smallImages.push_back( cv::Mat( image, rect ).clone() );
        cv::imshow( image_name[ i ], smallImages[ i ] );
    }
waitKey( 0 );

Otherwise, you display every image in the same window, therefore, you only see the last one. By the way, don't forget the waitKey, otherwise, you won't see anything! (but I guess you already have it?)

edit flag offensive delete link more

Comments

I have in my code waitkey (0) ; but I have an error: declaration of ‘image_name’ as multidimensional array must have bounds for all dimensions except the first for the declaration of image_name !!

abir gravatar imageabir ( 2014-06-20 03:43:38 -0600 )edit

Sorry, I've forgotten a dimension… I've updated the code.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2014-06-20 04:25:46 -0600 )edit

thank you but its not working

abir gravatar imageabir ( 2014-06-20 06:19:58 -0600 )edit

Could you be more specific: what is not working? The code I've provided or your initial issue?

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2014-06-20 07:53:49 -0600 )edit

the new solution does not working . error in line imshow ( image_name [ i ], smallImages [ i ] ); and if I change this line by imshow ( image_name [ i ], cv :: Mat ( image , rect ) ); I got only the last image but i like to have all the value of images saved in a table.

abir gravatar imageabir ( 2014-06-20 08:02:41 -0600 )edit

Did you get one image shown or many with the same data?

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2014-06-20 08:14:37 -0600 )edit

many small images of the input image

abir gravatar imageabir ( 2014-06-23 01:52:43 -0600 )edit

When you push back the image, you only push a reference. If you want to copy it, use the clone method: cv::Mat ( image , rect )cv::Mat ( image , rect ).clone()

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2014-06-23 03:09:32 -0600 )edit

If you can do for me a small sample code for an image and divide it into 4 parts and then save the 4 small images. Because I can not follow you. I am a beginner and I'm really blocked for several days. Please update your code with a simple and complete example thank you

abir gravatar imageabir ( 2014-06-23 04:53:13 -0600 )edit

I've updated the code.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2014-06-24 02:44:46 -0600 )edit

Question Tools

Stats

Asked: 2014-06-20 02:56:42 -0600

Seen: 351 times

Last updated: Jun 25 '14