Ask Your Question
2

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

asked 2014-05-13 10:28:41 -0600

abir gravatar image

updated 2017-10-09 01:56:11 -0600

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;

image = imread("crop_uEyeImg1.tif",1);
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);
/// Generate grad_x and grad_y
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
Mat grad;

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



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

///// Gradient Y
//// Scharr( image, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel( image, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_y, abs_grad_y );
///// Total Gradient (approximate)

addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );

namedWindow("ImageSobel", CV_WINDOW_AUTOSIZE );
imshow( "ImageSobel", grad );

namedWindow("ImageSobelGx", CV_WINDOW_AUTOSIZE );
imshow( "ImageSobelGx", abs_grad_x );

namedWindow("ImageSobelGy", CV_WINDOW_AUTOSIZE );
imshow( "ImageSobelGy", abs_grad_y );

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

Mat orientation = Mat::zeros(abs_grad_x.rows, abs_grad_y.cols, CV_32F); //to store the gradients

grad_x.convertTo(grad_x,CV_32F);
grad_y.convertTo(grad_y,CV_32F);
//phase(grad_x, grad_y, orientation);
//cv::normalize(orientation, orientation, 0x00, 0xFF, cv::NORM_MINMAX, CV_8U);

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

phase(grad_x, grad_y, orientation, true);
ofstream file1("orient.txt"); file1 << orientation; file1.close();
  }
}
waitKey(0);
return 0;
}
edit retag flag offensive close merge delete

Comments

1

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

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

2 answers

Sort by ยป oldest newest most voted
8

answered 2014-05-13 12:06:17 -0600

isarandi gravatar image

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.

edit flag offensive delete link more

Comments

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

berak gravatar imageberak ( 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 ); } }

abir gravatar imageabir ( 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.

berak gravatar imageberak ( 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

abir gravatar imageabir ( 2014-05-14 03:31:49 -0600 )edit
-1

answered 2014-05-13 10:31:37 -0600

wuling gravatar image

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

edit flag offensive delete link more

Comments

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

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

Question Tools

Stats

Asked: 2014-05-13 10:28:41 -0600

Seen: 32,310 times

Last updated: May 14 '14