简体   繁体   中英

OpenCV return only red shapes image

I´m working with OpenCV and I have an image which has red, black and white colours. Specifically it's a QR code sourronded by a red square. What I wanna do is to dismiss the QR code and just keep the red square. This is what i've been trying to do:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main(){
    Mat imagen, imagenThreshold, imagenThreshold2;
    vector<Mat> canales;
    imagen = imread("qr.ppm");
    split(imagen,canales);
    imshow("RGB", canales[2]);
    return 0;

}

But after I run it, what I have left is the QR code WITHOUT the red square, exactly the opposite of what I want.

What am i doing wrong?

thanks

You didn't post the image you were working on, so I generated a random test image. (Not a QR code, just random color blocks.) My answer relies on the fact that there are only 3 colors in the image: pure black, pure white, and pure red as defined below. Other colors or shades of these colors will require more processing.

On the left is my source image. In the middle is just the red channel of the source, the equivalent of your canales[2] . On the right is the red channel with all of the white blocks removed, our goal.

源图像源红色通道只有红色

As you can see, the second image has white wherever the original image has red or white. That's because, as @prompt said in the other answer, pure white is made by setting all three channels to their maximum value.

Black = BGR(0, 0, 0)
Red = BGR(0, 0, 255)
White = BGR(255, 255, 255)

So we want only the pure red blocks. By knowing that we only have red and white (and no magenta or yellow or whatever) we can can take a shortcut and simply subtract the pixels that have blue in them (or green) from the red channel:

  imshow("RGB", canales[2] - canales[0]);

You can also take this red-only channel and merge it into a BGR image with all-zero blue and green channels to give you the output here, which I think makes even more clear that the white pixels have been removed:

源图像只有红色

Probably you missunderstand what the function split exactly do.

It simply get your original image and then create three separated vector containing the specific values for each RGB channel.

Everything is white in the original image will be present in all the tree vectors. What is 100% red will be only in the 1st vector, what 100% green in the 2nd and what is 100% blue in the 3rd.

But what is white will be visible in all of them because as you probably know the white color is composed by a 0xFF of red, 0xFF of green and 0xFF of blue.

I suggest you to use the function inRange that do exactly what you need to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM