0

i'm new to opencv and use opencv3 .I'm trying to detect circles using hough-transform .i have this code which read image from a file and detect circles then write it to a file.this works fine .here is the original image and here is the detected one.

but i want to detect circles in a buffered image .so i used a method to convert buffered image to Mat object .then what happened is circle detection failed and image has been resized .also brightness has reduce too much.this is failed one.

here is the code i used to convert buffered image to mat ( taken from the stackoverflow answer)

public Mat bufferedImageToMat(BufferedImage bi) {
    byte[] pixels = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
    Mat mat = new Mat(bi.getWidth(), bi.getHeight(), CvType.CV_8UC3);
    mat.put(0, 0, pixels);
    return mat;
}

i think problem is in above method. this is code line 47 and 48.

Mat source = Imgcodecs.imread(circleimage, Imgcodecs.CV_LOAD_IMAGE_COLOR);
//Mat source = bufferedImageToMat(bi);

if i use 1st one(directly read from image) code works .but if i use 2nd one circle detection fail.

can you see a problem in this method ? thanks

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • 2
    in C++ syntax Mat constructor uses the parameters in different order: `Mat(rows, cols, type)`, probably that's same for java syntax? Please try `new Mat(bi.getHeight(), bi.getWidth(), CvType.CV_8UC3);` – Micka Feb 12 '16 at 12:28
  • 1
    @Micka ohh yeah you are right .tnx you so much.post it as answer plz.it works now – Madhawa Priyashantha Feb 12 '16 at 12:34

1 Answers1

1

In OpenCV, images are represented by matrices, whose constructors use number of rows before number of columns, so you should use

Mat mat = new Mat(bi.getHeight(), bi.getWidth(), CvType.CV_8UC3);

I've tried to explain the reason in OpenCV Point(x,y) represent (column,row) or (row,column) if you would like to know more about it.

Community
  • 1
  • 1
Micka
  • 19,585
  • 4
  • 56
  • 74