I want to get the maximum values from two images, while preserving other information.
For example, I have two images in HSL format, and I want the resulting image to consist of the brightest pixels. (this is an example, in reality I can’t convert to RGB, finding max, and converting back)
So I cannot use cvMax because it will separately give me the maximum H, maximum S and maximum L.
What I need is an image with the maximum L from either image, accompanied by the H and S values (or other information) from the same image.
For example, consider these 2×1 pixel images, with their HSV scalars:
IplImage* img1 = cvCreateImage(cvSize(2, 1), IPL_DEPTH_32F, 3);
cvSet2D(img1, 0, 0, cvScalar(1, 2, 45)); // Remember: HSV values
cvSet2D(img1, 0, 1, cvScalar(3, 4, 123));
IplImage* img2 = cvCreateImage(cvSize(2, 1), IPL_DEPTH_32F, 3);
cvSet2D(img1, 0, 0, cvScalar(5, 6, 114));
cvSet2D(img1, 0, 1, cvScalar(7, 8, 33));
IplImage* result = cvCreateImage(cvSize(2, 1), IPL_DEPTH_32F, 3);
//Do something to get the brightest pixels
CvScalar px0 = cvGet2D(result, 0, 0);
CvScalar px1 = cvGet2D(result, 0, 1);
cout << px0.val[0] << " " << px0.val[1] << " " << px0.val[2] << endl;
cout << px1.val[0] << " " << px1.val[1] << " " << px1.val[2] << endl;
Output should become:
5 6 114
3 4 123
Is there a neat ‘OpenCV’ way of doing this, instead of making my own, possibly slower, algorithm that parses each pixel?
Just an idea, but can I do something like this?
- Split them into 6 grayscale images, (actually, I already have this situation, so I don’t need to split)
- Take both V images,
- Generate a per-element map (1U or 8U image) where a ‘0’ or ‘1’ value means the highest value was in the first or second image respectively.
- Use this to combine selected pixels from the other planes.
The bold step is where I’m stuck.
There is no build-in opencv function to do it, but it’s not thah difficult to write your own.
You can simply
Of course, some little optimization can be done, like precalculating j+w*i, or using pointer access instead of array indexes.