简体   繁体   English

如何确定RGB或BGR中3通道的值

[英]How to determine the value of the 3 channel in RGB or BGR

This is the image that i need to detect 这是我需要检测的图像

在此处输入图片说明

//  cdst is the image
//    This is the code which i want to know what are the values in a certain pixel but the code below only detects the first then the 2nd and 3rd is equal to 0 

    void dec()
    {
        Mat em;
        cdst="path of the image";

        //this is the value of BGR per color that i wish to check
        Vec3f red(0, 0, 255);
        Vec3f blue(255, 0, 0);
        Vec3f green(0, 128, 0);
        Vec3f yellow(0, 255, 255);
        Vec3f marron(0, 0,128);
        Vec3f pink(147, 20, 255);
        Vec3f indigo(130, 0, 75);
        Vec3f midblue(112, 25, 25);
        Vec3f magenta(139, 0, 139);
        //em will hold the cdst image
        em=cdst;
        //for loop to determine what are the colors
        for (int i = 0; i < l.size(); i++)
        {
            int x = l[i][0];
            int y = l[i][1];
            cout << x << " " << y<<endl;
            Vec3f px = em.at<uchar>(y,x);

           //Im trying to print all color of the 3 channels
           //But it only the first on has a value then the second and third is 0
            cout << px.val[0] << " " << px.val[1] << " "<<px.val[2]<<endl;
            if (px == pink)
            {
                cout<<"A";
            }
        }
    }

How about try this code? 尝试这段代码怎么样?

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main(){

    Mat em(100, 100, CV_8UC3, Scalar::all(0));
    rectangle(em, Rect(30, 30, 10, 10), Scalar(147, 20, 255), -1);

    imshow("em", em);
    waitKey(1);

    Vec3b pink(147,20,255);
    for (int i = 0; i < em.rows; i++){
        for (int j = 0; j < em.cols; j++){
            Vec3b px = em.at<Vec3b>(i, j);
            cout << px << endl;
            if (px == pink){
                cout << "A" ;

            }
        }
    }

    waitKey(0);
    destroyAllWindows();
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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