Ask Your Question
1

Drawing lines, rectangles on Mat using Java wrapper stuck at 0,0?

asked 2013-08-16 19:31:40 -0600

jamescharters gravatar image

updated 2013-08-17 03:35:17 -0600

Hi All,

I am using OpenCV 2.4.6.1 on OSX 10.8.4 using the Java API with IntelliJ. I am developing a very simple application that works on a 640x480 24bpp Mat object which is an image read in from disk.

I have code to draw rectangles and lines, but despite changing the point coordinates for start and end only a single pixel is drawn at 0,0 of the Mat. The lines and rectangles seem to ignore any combination of coordinates I give and always result in this strange result. Here is some code:

Mat color_origImg = Highgui.imread("/Users/jamescharters/Downloads/sample_0_bw_hog-rect.jpg");
Core.line(color_origImg, new Point(50,50), new Point(10, 100), new Scalar(255,0,0), 1);
Highgui.imwrite("/Users/jamescharters/Downloads/sample_0_bw_hog-rect.jpg", color_origImg);

The image is definitely being read in correctly but I can't for the life of me figure out why plotting lines, rectangles or other sorts of shapes on a Mat object doesn't work as expected. I've tried black and white images, using CvtColor too ... all to no avail.

Can anyone offer some advice?

edit retag flag offensive close merge delete

Comments

Possible you are reading it in as a 3 channel color image, and the drawing functions assume a single channel mat element. (just a guess here) Can you try reading your image as grayscale image? Then apply the drawing? If this works, to keep your color image, split the channels first, draw, then recombine. But it seems strange to me .. I thought we could draw on 3 channel images without problems...

StevenPuttemans gravatar imageStevenPuttemans ( 2013-08-17 03:40:07 -0600 )edit

Hi Steven, thank you for the reply. I've tried reading the image in as greyscale and drawing. No luck so far!

jamescharters gravatar imagejamescharters ( 2013-08-20 05:07:05 -0600 )edit

It is actually pretty weird. I am doing the exact thing and it seems to work for me. However I am using the C++ interface and haven't tested the Java API jet. So no idea what to expect. I will post some dummy code on how I do this in C++.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-08-20 05:11:18 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-08-20 05:47:19 -0600

What I do in C++ is the following. Maybe it can help to try and translate this to Java API.

// Create a dummy container for copying the original frame into
Mat image_copy;
// Copy over the image data
image_original.copyTo(image_copy);
// Draw the rectangle onto the copy
rectangle(image_copy, Point(roi_x0,roi_y0), Point(x,y), CV_RGB(255,0,0));
// Renew the window with the image
imshow(window_name, image_copy);

By copying the image into a dummy one, I make sure that the original one is not altered but the drawings are saved only for this local copy. I don't know if this would have effect to your case.

edit flag offensive delete link more

Comments

1

Hi Steven, thanks so much for the C++ example. I will try this approach in both C++ and Java myself and report back on what the results are shortly

jamescharters gravatar imagejamescharters ( 2013-08-20 18:11:49 -0600 )edit
1

Hi Steven, sorry for the late reply. Your approach in C++ works as expected, but the JVM segfaults for some reason when I run the Java equivalent. I will do a bit more investigation tomorrow when I have some time...

jamescharters gravatar imagejamescharters ( 2013-08-30 05:11:51 -0600 )edit

Okay, if the problem keeps existing, making a bug report with details over your trials can help us solve problems :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-08-30 05:42:42 -0600 )edit
1

Yep I definitely will. Thank you for the timely help and advice with this issue!

jamescharters gravatar imagejamescharters ( 2013-08-30 07:07:10 -0600 )edit

Question Tools

Stats

Asked: 2013-08-16 19:31:40 -0600

Seen: 8,212 times

Last updated: Aug 20 '13