# How to cut an image in small images with opencv !!!

C:\fakepath\small image.jpgHello I need to cut an image in small images with opencv for my algo ! please help me . 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;

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

/// Convert it to gray
//cvtColor( image, image, CV_RGB2GRAY );
//resize(image,image,Size(0,0),0.5,0.5,INTER_LINEAR);
namedWindow("Image", CV_WINDOW_AUTOSIZE );
imshow("Image", image);
int scale = 1;
int delta = 0;
int ddepth = CV_16S;

// 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 ;

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 ));

////Scharr( image, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
Sobel( image, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );

//// Scharr( image, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel( image, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );

namedWindow("ImageSobel", CV_WINDOW_AUTOSIZE );

namedWindow("ImageSobelGx", CV_WINDOW_AUTOSIZE );

namedWindow("ImageSobelGy", CV_WINDOW_AUTOSIZE );

// // Calculate orientations of gradients --> in degrees
// //Loop over all matrix values and calculate the accompanied orientation

//cv::normalize(orientation, orientation, 0x00, 0xFF, cv::NORM_MINMAX, CV_8U);

////namedWindow("Orientation", CV_WINDOW_AUTOSIZE );
////imshow( "Orientation", orientation );

ofstream file1("orient.txt"); file1 << orientation; file1.close();
}
}
waitKey(0);
return 0;
}

edit retag close merge delete

1

next time, cut down on exclamation marks, please ..

( 2014-05-13 12:39:00 -0600 )edit

Sort by » oldest newest most voted

You can extract rectangular regions from a Mat.

cv::Mat bigImage = cv::Mat::zeros(cv::Size(660,350));
cv::Mat smallImage = cv::Mat(bigImage, cv::Rect(0,0,110,70));


cv::Rect takes the x, y, width, height as constructor parameters.

Note that this will not copy the image data, it just creates another wrapper to the same image data. If you need data copy, use

cv::Mat smallImage = cv::Mat(bigImage, cv::Rect(0,0,110,70)).clone();


Now if you need to extract multiple such image parts, essentially slicing your image with a grid, you can create a for loop as follows.

cv::Size smallSize(110,70);
std::vector<Mat> smallImages;

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


Here I assumed that the size of the bigImage is an integer multiple of the size of the desired small images.

more

abir, this time, you'll have to edit your post above, and show the code you're using.

( 2014-05-14 02:22:25 -0600 )edit

Yes , I need to extract multiple such image parts . when I execute the program I have no errors and if I add 2 lines to display small images. I do not display !! where is the problem ? please help me !!

cv::Size smallSize(110,70); std::vector<Mat> smallImages;

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

namedWindow("ImageSobel", CV_WINDOW_AUTOSIZE ); imshow( "Imagette", smallImages ); } }

( 2014-05-14 02:55:47 -0600 )edit

imshow() always needs waitKey(some_millis);, else nothing gets drawn.

also you can't draw the whole vector, only single images.

( 2014-05-14 03:02:10 -0600 )edit

Hello, For my project I need to develop a study appilcation which allows us to obtain the orientation of the content of the image angle For this I used the gradient method which allows us to recover the angle. To carry out this program I have cut the original image into small images (this code is in 30 small images) and then I want to display each of the small images and get the orientation angle. for this program if I use the original image without cutting small picture I get a text file with the angle of orientation. If I add 2 loops to cut the intiale image into small images it works not. Please help me . thank you

( 2014-05-14 03:31:49 -0600 )edit

1.Create an new image 2. use findcontours to get contours and calcuate area size 3. if area size is less than constant size then do nothing else copy an new image

more

I don't need to findcontours of image but to crop image 1 to many other image for exemple Image 1 have a size (660x350) and the 30 image have a size 110x70 !! thank you

( 2014-05-13 10:39:46 -0600 )edit

Official site

GitHub

Wiki

Documentation