input Mat used in goodFeatureToTrack - java

I'm trying to detect feature in face with goodFeatureToTrack function in java. It always crashes there. This is my code:
Mat eyeRegion = detectCascade();//i used here cascadeclassifier to detect eye region
if(eyeRegion.empty())
Log.i("face_calibrated","eye region is empty");
I make sure that my region is not empty.
if(eyeRegion.channels()==1)
{
//i convert my mat to rgb to detectfeatures
Imgproc.cvtColor(eyeRegion, eyeRegion, Imgproc.COLOR_GRAY2RGB);
Log.i("face_calibrated","eyeRegion is channel1");
}
Here is the problem:
Imgproc.goodFeaturesToTrack(eyeRegion,cornersA,4,0.01,5.0,null,3,false,0.04);
I have declared cornersA as global MatOfPoint
public MatOfPoint cornersA = new MatOfPoint();
What type of input mat (RGB or GRAY) should be used in this function?

Related

Determine the color space of a Mat Opencv

I am working on a program where I have these contours I am looking at and I want to find the RGB values of each specific contour. The current problem I am facing is that I the mat is of CvType CV32_F . I change the images rtype to CV_8UC3. I want to know what color space the Mat is in so that I can convert it to the RGB spectrum. I never set the Mat to a color space so I am not sure which one it is. So I get conversion errors. I have tried BGR, GRAY and RGB2BGR( just to see if it was already in that spectrum and would change). Here is my code:
private static void findRGBs(List<MatOfPoint> squareContours){
for(int i = 0; i < squareContours.size(); i++){
squareContours.get(i).convertTo(squareContours.get(i), CvType.CV_8UC3);
Imgproc.cvtColor(squareContours.get(i), squareContours.get(i), Imgproc.COLOR_BGR2RGB);
//not sure what color space this image is
getPixelValues(squareContours.get(i));
foundFrames.add(squareContours.get(i));
}
}

Implementation missing for Moments function von openCV

I'm trying to develop an application with android studio to capture the color of flashing LEDs.
Im now at this Point, that i got the positions of the LEDs and now i want to determine the bounding box center with the class moments of openCV.
Mat src = new.Mat(Bitmap.getHeight(), Bitmap.getWidth(), CvType.CV_8UC1);
Mat hsv = new Mat();
Utils.bitmapToMat(Bitmap, src);
Imgproc.cvtColor(src,hsv, Imgproc.COLOR_BGR2HSV);
Core.inRange(hsv,LOWER_RED,HIGHER_RED,hsv);
Imgproc.dilate(hsv,hsv,Imgproc.getStructuringElement(Imgproc.MORPH_VECT,new Size(50,50)));
List<MatOfPoint> contours = new ArrayList <>();
Mat hierarchy = new Mat();
Imgproc.findContours(hsv, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
hierarchy.release();
//finding Bounding Box Center
List<Moments> mu = new ArrayList<>(contours.size());
mu.add(1, Imgproc.moments(contours.get(1), false));
Utils.matToBitmap(hsv,Bitmap);
//...
And the lines with Moments brings me following error message:
No implementation found for double[] org.opencv.imgproc.Imgproc.moments_0(long, boolean) ( tried Java_org_opencv_imgproc_Imgproc_moments_10 and Java_org_opencv_imgproc_Imgproc_moments_10__JZ )
So if i jump to Imgproc.java then the method is red marked and following message popups by mouseover:
Cannot resolve corresponding JNI function Java_org_opencv_imgproc_Imgproc_moments_10
Reports native method declaration in Java where no corresponding JNI functions is found in the project.
Thanks for reading
Can anyone help me ?

color detection using opencv

I am trying to extract the title bar from the image below using opencv.
I am using the following code -
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat original = Imgcodecs.imread("D:/test_bg_extract.png", Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
Mat hsvMat = new Mat();
Imgproc.cvtColor(original, hsvMat, Imgproc.COLOR_BGR2HSV);
Mat dst = new Mat();
Core.inRange(hsvMat, new Scalar(23,231,205), new Scalar(23,231,205), dst);
I used a color picker to determine the HSV value of the title bar (from the hsvMat converted to display image). When the run the code to display the output, I see a blank screen. I can't tell what I am doing wrong. Am I picking the wrong HSV color? If I know the exact RGB code then should I even be converting image to HSV? (this however seems to be the standard method from many code examples on the internet).
Ok, I solved it using RGB based color, switching to BGR while specifying the color that I need. So this worked -
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat original = Imgcodecs.imread("D:/test_bg_extract.png", Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
Mat dst = new Mat();
Core.inRange(original, new Scalar(132,178,205), new Scalar(132,178,205), dst);

Android Watershed OpenCV

My Question how to count Objects in image on Android Watershed OpenCv?
Since you didn't tell us which line causes those problem, but i strongly assume you misused erode() and dilate().
You should pass 3rd parameter of each function with valid non-zero Matrix.(e.g. Kernel)
erode & dilate example
// You can change the size of kennel
// if you want to change the range of erosion & dilation
Mat kelnel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2,2));
Mat fg = new Mat(img.size(),CvType.CV_8U);
Imgproc.erode(threeChannel, fg, kelnel);
Mat bg = new Mat(img.size(),CvType.CV_8U);
Imgproc.dilate(threeChannel, bg, kelnel);

Imgproc.drawContours(....) not showing up

In my android app I get an image from the gallery as a bitmap with something like this
Bitmap bitm = getMyImage("Thanks!");
and I have a Mat called mat declared like this:
Mat mat = new Mat(bitm.getHeight(), bitm.getWidth(), CVType.CV_8UC3);
I'm trying to get contour areas from the image, which I've successfully gotten then draw it back on the original image with:
Imgproc.drawContours(mat, contours, -1, new Scalar(200,200,0), 2);
displayMat(mat);
If I use it like that, it works but the contours are drawn on a blank image which isn't what I want. I want it to be drawn on the original image. If I use
Utils.bitmapToMat(bitm, mat);
before the previous snippet of code, the displayed image is just the preprossed image without the 'Drawn' contours. Why?
Asfaik Android uses images with alpha values, so CV_8UC4 is the right data type.
So
Mat mat = new Mat(bitm.getHeight(), bitm.getWidth(), CVType.CV_8UC3);
Imgproc.drawContours(mat, contours, -1, new Scalar(200,200,0), 2);
displayMat(mat);
draws the contours correctly on an empty/blank 8UC3 image (if memory empty).
But if you want to draw on the input image by first converting Utils.bitmapToMat(bitm, mat); you'll overwrite your 8UC3 memory and replace it by 8UC4 data. After that you draw Scalar(200,200,0) which will use a 4th channel, but cv::Scalar automatically adds those channels with default zero values, so you draw your contours in transparent. So use Scalar(200,200,0,255) instead and it should give your expected results.
Mat mat = new Mat(bitm.getHeight(), bitm.getWidth(), CVType.CV_8UC3);
Utils.bitmapToMat(bitm, mat);
Imgproc.drawContours(mat, contours, -1, new Scalar(200,200,0,255), 2);
displayMat(mat);
The other method would be to convert the bitmap to 8UC3, but I'm not sure how to do that.

Categories