Ask Your Question

trouble understanding pixel access

asked 2018-01-08 16:56:36 -0500

gordano gravatar image

updated 2018-01-08 16:59:12 -0500

I am just getting into opencv and set up a demo just to get familiar.

I made a function to get and set pixel values, but it doesn't behave how I would expect. In this case I converted the Mat to RGB, but the only channel I can seem to effect is img.[1], which does alter the green component. Anything else, however doesn't work. I converted a mask Mat which is the output of an edge detector to RGB, and then also tried converting that to HSV and changing the img.[0] hue value, but that doesn't seem to do anything.

I am asking because even though my application is just experimenting, it seems like I am missing something about how to work with colors, and how values are contained in MAts.

For instance, if I just wanted to scan through the edge detector output and change the white pixels to random hue values how would I do that?

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/video.hpp"
#include <windows.h>
#include <iostream>

using namespace cv;
using namespace std;

inline void processBgs(const Ptr<BackgroundSubtractor>& p, Mat& f, Mat& fg, double rate){
     p->apply(f, fg, rate);

inline void processCanny(Mat& img, Mat& edges, int lowthresh, int hithresh, int sobelsize){
     Canny(img, edges, lowthresh, hithresh, sobelsize);

// I'm attempting to change pixels to any color here
void rainbow(Mat& img){
    for(int i=0; i<img.rows; i++){
        for(int j=0; j<img.cols; j++){

       Vec3b & color =<Vec3b>(i,j);
        color[0] = 200;


int main(int argc, char** argv)
        VideoCapture cap(0);

        namedWindow( "Display window", CV_WINDOW_AUTOSIZE );
        HWND hwnd = (HWND)cvGetWindowHandle("Display window");

        Mat frame;
        Mat fgMask; 

        Ptr<BackgroundSubtractor> bgs = createBackgroundSubtractorMOG2();

        cap >> frame;

        cout << frame.type();
        imshow("Display window", frame);  

        while(IsWindowVisible(hwnd)) {
            cap >> frame;
            if(frame.empty()){ break; }
            processBgs(bgs, frame, fgMask, 0.01);
            processCanny(fgMask, fgMask, 50, 150, 3);
            cvtColor(fgMask, fgMask, CV_GRAY2RGB);
          //  cvtColor(fgMask, fgMask, CV_RGB2HSV);
            imshow("Display window", fgMask);     
            if(cvWaitKey(5) == 27){ break; }

        return 0;
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-01-08 19:31:10 -0500

sjhalayka gravatar image

updated 2018-01-08 19:59:16 -0500

To assign a colour, you can use the index to specify blue, green, or red:

// orange pixels<Vec3b>(i, j)[0] = 0;<Vec3b>(i, j)[1] = 127;<Vec3b>(i, j)[2] = 255;

Why do you use a reference?

edit flag offensive delete link more


Ok, I see the reference wasn't neccesary, your code works, I'm not sure what my problem was earlier since I tried nearly the same thing. One thing I cannot get to work still is changing the hue value. If I convert to hsv, the white pixels become red, but changing the first element in the pixel does not change anything.

gordano gravatar imagegordano ( 2018-01-08 20:42:56 -0500 )edit

Update: I solved it: It seems the image needed to be reconverted to BGR to display correctly in imshow

gordano gravatar imagegordano ( 2018-01-08 21:35:57 -0500 )edit

I'm glad that things are going your way now. :)

sjhalayka gravatar imagesjhalayka ( 2018-01-08 21:40:09 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-01-08 16:56:36 -0500

Seen: 77 times

Last updated: Jan 08 '18