Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

trouble understanding pixel access

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);
}

void rainbow(Mat& img){
    for(int i=0; i<img.rows; i++){
        for(int j=0; j<img.cols; j++){

       Vec3b & color = img.at<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);
          //  rainbow(fgMask);
            imshow("Display window", fgMask);     
            if(cvWaitKey(5) == 27){ break; }
        }

        waitKey(0); 
        return 0;
}

trouble understanding pixel access

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++){ i++){="" for(int="" j="0;" j<img.cols;="" j++){<="" p="">

       Vec3b & color = img.at<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);
          //   rainbow(fgMask);
            imshow("Display window", fgMask);     
            if(cvWaitKey(5) == 27){ break; }
        }

        waitKey(0); 
        return 0;
}

trouble understanding pixel access

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++){<="" p="">

i++){
        for(int j=0; j<img.cols; j++){

       Vec3b & color = img.at<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);
            rainbow(fgMask);
            imshow("Display window", fgMask);     
            if(cvWaitKey(5) == 27){ break; }
        }

        waitKey(0); 
        return 0;
}