Converts Opencv hsv to the equivalent of Matlab HSV
I had to develop android code using opencv, which is equivalent to MATLAB.
Because I have to read the image and convert it to HSV, I’ve used:
Imgproc.cvtColor(temp, hsv , Imgproc.COLOR_RGB2HSV);
When I iterate over every pixel value of a pixel returned in the range 0-255 of Hue, in Matlab it returns a range of 0-1, and I don’t know how to write equivalent code for it.
I’m a beginner and don’t know much about image processing.
Solution
Obviously, the value range of RGB images is [0, 255]. For HSV images, it depends on the image type (see OpenCV doc):
- 8-bit images: H in [0, 180] and S,V in [0, 255].
- 32-bit images: H in [0, 360], S, V in [0,1].
So, after you convert to HSV
Imgproc.cvtColor(temp, hsv, Imgproc.COLOR_RGB2HSV);
You need to scale the H and S,V values differently. You can use split and merge to get different channel matrices, and apply the correct scaling.
Note that OpenCV stores RGB images as BGR, so you may need to COLOR_BGR2HSV
.
You can split and merge like this:
List<Mat> planes = new ArrayList<Mat>(3);
Core.split(hsv, planes);
Scale each single plane
Core.merge(planes , hsv);