Ask Your Question
1

How to split an image into blocks images with opencv ?

asked 2014-05-14 04:48:39 -0600

abir gravatar image

updated 2014-05-14 08:10:11 -0600

Hello , please find attached the original image and another with cutting. I need your help

C:\fakepath\image.jpg(/upfiles/14000608217059684.jpg)

C:\fakepath\blocksimage.jpg

thank you

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"

using namespace std;
using namespace cv;

int main()
{
Mat image;

image = imread("crop_uEyeImg1.tif",1);
if(image.empty())
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}


namedWindow("Image", CV_WINDOW_AUTOSIZE );
imshow("Image", image);


   // get the image data
 int height = image.rows;
 int width = image.cols;

 printf("Processing a %dx%d image\n",height,width);

cv :: Size smallSize ( 110 , 70 );

std :: vector < Mat > smallImages ;
namedWindow("smallImages ", CV_WINDOW_AUTOSIZE );

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 ));
        waitKey(0);
    }
}


return 0;
}
edit retag flag offensive close merge delete

Comments

dear, you're still trying to imshow() a whole vector of images. it only works with a single one

berak gravatar imageberak ( 2014-05-14 07:12:47 -0600 )edit

even if I put imshow to the outside of the loop does not work!!

abir gravatar imageabir ( 2014-05-14 07:23:37 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2014-05-14 06:48:47 -0600

unxnut gravatar image

updated 2014-05-14 08:21:18 -0600

Didn't a question like this appear yesterday as well here? Seems like homework assignment from some class. In your case, you need to move the namedWindow call before the loop and waitKey call just after imshow inside the loop.

In your imshow, you should display the last image you added to the vector, instead of the whole vector. So, your call should be: imshow ( "smallImages", cv::Mat ( image, rect ));.

edit flag offensive delete link more

Comments

yes with this modification I display the last image :) but I need to display all image !! what can I do !! thank you

abir gravatar imageabir ( 2014-05-14 07:05:14 -0600 )edit

yes with this modification I display the last image :) but I need to display all image !! what can I do !! thank you

abir gravatar imageabir ( 2014-05-14 07:46:54 -0600 )edit

Your namedWindow call should be before the for loop. And imshow and waitKey should be inside the for loop.

unxnut gravatar imageunxnut ( 2014-05-14 08:07:00 -0600 )edit

I checked it traverses the two loops,so I can not display all smallImage. But , if I want to do a treatment on Smallimages . How I call it !! thank you

abir gravatar imageabir ( 2014-05-14 08:33:17 -0600 )edit

What treatment? Please explain.

unxnut gravatar imageunxnut ( 2014-05-14 10:02:02 -0600 )edit

treatment is applied on each small image gradient algorithm for the orientation of each small image angle .

abir gravatar imageabir ( 2014-05-14 10:47:37 -0600 )edit

Your question is still not clear. Has your original problem been solved yet, to extract and display each subimage?

unxnut gravatar imageunxnut ( 2014-05-14 11:02:16 -0600 )edit

the problem is not solved yet :/ I can only display the last image .. I want to use the small images to calculate the orientation angle using the gradient method with this rule θ = arctan (Dy(x, y)/Dx(x, y)) ??? 

abir gravatar imageabir ( 2014-05-15 01:44:51 -0600 )edit

Question Tools

Stats

Asked: 2014-05-14 04:48:39 -0600

Seen: 15,058 times

Last updated: May 14 '14