Ask Your Question

accessing row and col pixel array

asked 2017-06-28 03:40:16 -0600

jjabaram gravatar image

image description

image description

for (int j = 0; j<image.rows; j++)
    for (int i = 0; i<image.cols; i++)
        if (<uchar>(j, i) < 100) //change color less than 100
  <uchar>(j, i) = 255; // make it white

i want to change all the color < 100 to white, but only less than half of the image that change, is it something wrong with row and col calculation ?.

and how to select specific color in the image and change it to red.

Thank you.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2017-06-28 04:11:08 -0600

LBerger gravatar image

updated 2017-06-28 05:33:16 -0600

You should use mask :

    Mat image=imread("f:/lib/opencv/samples/data/lena.jpg",IMREAD_GRAYSCALE);
    Mat mask = image<100;

if you want to change a gray level in red you have to convert your image first

    Mat imageColor;
    cvtColor(image, imageColor,CV_GRAY2BGR);
    imshow("rc", imageColor);

If your image is in color use inRange :

    Mat image = imread("f:/lib/opencv/samples/data/lena.jpg", IMREAD_COLOR);
    Mat mask;
    image.setTo(Vec3b(0,0,255), mask);
    imshow("r", image);
edit flag offensive delete link more

answered 2017-06-28 05:11:19 -0600

kbarni gravatar image

First: your image is in RGB (CV_8UC3) and you treat it as a greyscale image. That's why it goes only to 1/3 of your row. RGB images contain 3xCols bytes (uchar values) per row.

So either you go to 3*cols in the second for loop, or use Vec3b variables.

With Vec3b variables it's also simple to set it in red:

Vec3b red=Vec3b(0,0,25);
    //in the second for loop
    Vec3b<Vec3b>(j, i)
    if ( (pixel[0]+pixel[1]+pixel[2]) /3 < 100) //change color less than 100
    {<Vec3b>(j, i) = red; // make it red

Second: if you want to process greyscale images, be sure they are CV_8UC1 type. Convert it if needed.

edit flag offensive delete link more

Question Tools



Asked: 2017-06-28 03:40:16 -0600

Seen: 1,479 times

Last updated: Jun 28 '17