Ask Your Question
0

I am trying to get all the different pixel values from an image and display the same?

asked 2018-06-26 08:53:08 -0600

mayra gravatar image

The problem is I can access pixel values from an image but how to compare them is where I am stuck!!!

Accessing pixel is simple, I am using the following code for this:-

Mat image = img;

for(int y=0;y<img.rows;y++ ){  
for(int x=0;x<img.cols;x++)
{
    // get pixel
    Vec3b color = image.at<Vec3b>(Point(x,y)); }
}
edit retag flag offensive close merge delete

Comments

don't do anything per pixel in opencv.

what are you trying to achieve ? what is the purpose of the "comparison" ?

tell us, maybe we can find a better way.

berak gravatar imageberak ( 2018-06-26 09:37:41 -0600 )edit

I am trying to get all the different colors in an image and get their RGB values, So I thought comparing pixels can give me that.

mayra gravatar imagemayra ( 2018-06-27 00:04:30 -0600 )edit

Thanks a lot for the reply, I will see this.

mayra gravatar imagemayra ( 2018-06-27 03:42:30 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-07-02 00:15:41 -0600

mayra gravatar image

I figured out the solution

#include "stdafx.h"
#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;

struct palette {
Vec3b Lower_range;
Vec3b Upper_range;
string name;
     };


int main(int argc, char** argv)
    {
struct palette range[8];


range[0].Lower_range = (170, 100, 0);
range[0].Upper_range = (180, 255, 255);
range[0].name = "Red";
range[1].Lower_range = (90, 50, 50);
range[1].Upper_range = (130, 255, 255);
range[1].name = "Blue";
range[2].Lower_range = (23, 41, 133);
range[2].Upper_range = (40, 150, 255);
range[2].name = "Yellow";
range[3].Lower_range = (40, 50, 50);
range[3].Upper_range = (70, 255, 255);
range[3].name = "Green";
range[4].Lower_range = (157, 50, 50);
range[4].Upper_range = (158, 255, 255);
range[4].name = "Pink";
range[5].Lower_range = (255, 255, 233);
range[5].Upper_range = (255, 255, 233);
range[5].name = "Black";
range[6].Lower_range = (10, 65, 40);
range[6].Upper_range = (20, 255, 255);
range[6].name = "Orange";
range[7].Lower_range = (28, 200, 20);
range[7].Upper_range = (30, 255, 233);
range[7].name = "Brown";
range[8].Lower_range = (0, 50, 0);
range[8].Upper_range = (10, 255, 255);
range[8].name = "Red";

vector< vector<Point> > contours;
vector<Vec4i> hierarchy;
Mat im = imread("blue_flower.jpeg");
Mat im_hsv;
cvtColor(im, im_hsv, COLOR_BGR2HSV);
Mat mask;
for (int i = 0; i < 9; i++) {

    inRange(im_hsv, Scalar(range[i].Lower_range[0], range[i].Lower_range[1], range[i].Lower_range[2]), Scalar(range[i].Upper_range[0], range[i].Upper_range[0], range[i].Upper_range[0]), mask); //Threshold the image
    findContours(mask, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    if (contours.size() > 0) {
        cout << range[i].name << " color detected" << endl;
    }
}
imshow("Original", im); //show the original image

if (waitKey(0) == 27)
{
}


return 0;

   }
edit flag offensive delete link more

Comments

You can use countNonZero instead of findContours

LBerger gravatar imageLBerger ( 2018-07-02 03:57:51 -0600 )edit

Yeah, I also thought of using this . The above code gives me false results.Thanks for the suggestion @LBerger

mayra gravatar imagemayra ( 2018-07-02 06:40:49 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-06-26 08:53:08 -0600

Seen: 1,030 times

Last updated: Jul 02 '18