Ask Your Question
0

How to crop a image in opencv

asked 2016-01-05 06:02:40 -0600

Denisa gravatar image

Hi,

i want to crop a image in OpenCV without using functions.

How can I do this?

A have a image with 2 objects. I want to show the original image with 2 objects and a windows with a image with 1 object.

Thank you.

edit retag flag offensive close merge delete

Comments

is the cropping done manually? the object is always in the same place? try to use a Rect like a ROI: cv::imshow("object", image(roi));

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-05 06:24:15 -0600 )edit

Hi, thank you for your answer, but i don't have the permission to use function. I only can use C++ code. My project is: I have a image with a coin (entry image) and a must display the coin in his real dimension.

I write only this code jet: void main(){ char imagine = "C:\Users\Silvia\Desktop\procesare imagini\images2\T4.png"; IplImageimg = cvLoadImage(imagine); CvScalar currPixel; IplImage*imgLUM = cvLoadImage(imagine); for (int y = imgLUM->height/1.3; y > 0; y--) { for (int x = imgLUM->width/1.3; x < imgLUM->width; x++) { currPixel = cvGet2D(img, y, x); currPixel.val[0] = 255; currPixel.val[1] = 255; currPixel.val[2] = 255; cvSet2D(imgLUM, y, x, currPixel); } } cvShowImage("Image", img); cvShowImage("ImageNew", imgLUM); cvWaitKey(0); } How can i make it?

Denisa gravatar imageDenisa ( 2016-01-06 08:20:06 -0600 )edit

:-O C-api?? get rid of it!!! Another thing: you can edit your question with the code and format it as code so we can better understand it ;)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-06 09:34:55 -0600 )edit

include "Tools.h"

void main(){
   char imagine = "C:\Users\Silvia\Desktop\procesare imagini\images2\T4.png";
   IplImageimg = cvLoadImage(imagine);
   CvScalar currPixel;
   IplImage*imgLUM = cvLoadImage(imagine);
   for (int y = imgLUM->height/1.3; y > 0; y--)
   {
      for (int x = imgLUM->width/1.3; x < imgLUM->width; x++)
      {
         currPixel = cvGet2D(img, y, x);
         currPixel.val[0] = 255;
         currPixel.val[1] = 255;
         currPixel.val[2] = 255;
         cvSet2D(imgLUM, y, x, currPixel);
      }
   }
   cvShowImage("Image", img);
   cvShowImage("Image2", imgLUM);
   cvWaitKey(0);
}
Sorry, was the first time that i posted on this forum.
Denisa gravatar imageDenisa ( 2016-01-06 11:53:55 -0600 )edit

It doesn't work to paste the code as code. Can you give me a emailadress? I can sent you the code if your welcome to help me with this university project

Denisa gravatar imageDenisa ( 2016-01-06 11:57:01 -0600 )edit

Hi, thank you for your answer, but i don't have the permission to use function --> euhm then how are you allowed to use OpenCV? OpenCV is a library of computer vision functions ...

StevenPuttemans gravatar imageStevenPuttemans ( 2016-01-07 02:49:04 -0600 )edit
2

@Denisa, please don't ask folks here to send you stuff privately. every useful comment/answer here should be available for anyone in need, right ?

berak gravatar imageberak ( 2016-01-07 04:39:06 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-01-07 02:35:27 -0600

thdrksdfthmn gravatar image

updated 2016-01-07 03:06:27 -0600

berak gravatar image

I am pretty sure that your problem is the C-api... apart that you are displaying a white image instead of crop; so:

1: Switch to C++:

void main(){
   std::string imagine = "C:\Users\Silvia\Desktop\procesare imagini\images2\T4.png";
   cv::Mat img = cv::imread(imagine);
   cv::Scalar currPixel;
   cv::Mat imgLUM = cv::imreadimagine);
   for (int y = imgLUM.rows/1.3; y > 0; y--)
   {
      for (int x = imgLUM.cols/1.3; x < imgLUM.cols; x++)
      {
          // ???
      }
   }
   cv::imshow("Image", img);
   cv::imshow("Image2", imgLUM);
   cv::waitKey(0);
}

2: Then do a crop:

cv::Mat image = cv::imread(imagine);
cv::Rect cropArea(image.cols/3, image.rows/3, image.cols/3, image.rows/3); // play with values to obtain what you need, now it is the middle of the image
cv::Mat croppedImage = image(cropArea);
cv::imshow("image", image);
cv::imshow("cropped image", croppedImage);
cv::waitKey();

3: If you want to remove the cropped area of the image, then just do

image(cropArea).setTo(cv::Scalar(0, 0, 0, 0));
edit flag offensive delete link more

Comments

1

@thdrksdfthmn , just for the record, code formatting does not really work inside bullet-lists (1: instead of 1. does the trick..)

berak gravatar imageberak ( 2016-01-07 03:08:33 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-05 06:02:40 -0600

Seen: 4,192 times

Last updated: Jan 07 '16