Ask Your Question
0

process specific pixels from an image

asked 2014-01-18 02:09:06 -0600

aparna gravatar image

d

How can i separate a specific region enclosed by the bounding box from the image and then process that specific region alone ??

edit retag flag offensive close merge delete

Comments

@JAyThaRevo never post links as solutions ... as they will possible disappear in the future this is not a good idea to make sure the solution is still available for other users when searching this forum. I will add a code snippet as answer.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-01-23 07:20:03 -0600 )edit

3 answers

Sort by ยป oldest newest most voted
0

answered 2014-01-23 19:03:06 -0600

keghn gravatar image

updated 2014-01-24 02:06:06 -0600

Hi,

I am new to OpenCV. I was thinking my be you could hit the pixels you want with this program:


`#`include`<opencv2/highgui/highgui.hpp>`

`#`include`<stdio.h>`

`#`include`<iostream>`

using namespace cv;
using namespace std;

int main()
{
    Mat img = imread("red.jpg",CV_LOAD_IMAGE_COLOR);

    unsigned char *input = (unsigned char*)(img.data);
    unsigned int i,j,r,g,b;
    unsigned int r_Old,g_Old,b_Old;
    unsigned int r_average,g_average,b_average;
    uchar blue, green, red;
// dead center image read of a pixels of my image.jpg



   unsigned int y, x;

    y = 300;
    x = 300;
    Vec3b intensity = img.at<Vec3b>(y, x);


//The same method can be used to change pixel intensities: img.at<uchar>(y, x) = 128;
//have not written to image yet.


    blue = intensity.val[0];
    green = intensity.val[1];
    red = intensity.val[2];

    b_Old = blue;
    g_Old = green;
    r_Old = red;

    cout << "\n";
    cout << "b_Old " << b_Old << "\n";
    cout << "g__Old " << g_Old << "\n";
    cout << "r__Old " << r_Old << "\n";

        b_average = b_Old;
        g_average = g_Old;
        r_average = r_Old;

// i scaned the whole screen and averaged for an average color value:


    for( y = 0;y < img.rows;y++)
    {
    for( x = 0;x < img.cols;x++)
        {

    Vec3b intensity = img.at<Vec3b>(y, x);
    blue = intensity.val[0];
    green = intensity.val[1];
    red = intensity.val[2];

            b_average = b_average + blue;
            g_average = g_average + green;
            r_average = r_average + red;

            }

        }
    cout << "\n";

    b_average = b_average/(img.rows * img.cols);
    g_average = g_average/(img.rows * img.cols);
    r_average = r_average/(img.rows * img.cols);

    cout << "b_average " << b_average << "\n";
    cout << "g_average " << g_average << "\n";
    cout << "r_average " << r_average << "\n";
    cout << "img.rows " << img.rows << "\n";
    cout << "img.cols " << img.cols << "\n";

    return 0;
}

g++ -ggdb `pkg-config --cflags opencv` -o `basename p.cpp .cpp` p.cpp `pkg-config --libs opencv`

http://docs.opencv.org/doc/user_guide/ug_mat.html

g++ p.cpp -o p `pkg-config --cflags --libs opencv`

Worked on Mint Linux 14 in the terminal.

green.jpg(/upfiles/13905252524741347.jpg)(/upfiles/13905252303553609.jpg)

edit flag offensive delete link more

Comments

Not to be rude, but your code does way more than what he actually asked for. Also, keep in mind in the future, that if a sortlike solution is given, it is better to add a comment with remarks, then readd a similar answer...

StevenPuttemans gravatar imageStevenPuttemans ( 2014-01-24 02:08:00 -0600 )edit
0

answered 2014-01-24 18:26:19 -0600

keghn gravatar image
Vec3b intensity = img.at<Vec3b>(y, x);
blue = intensity.val[0];
green = intensity.val[1];
intensity.val[2] = 128;//ya, I can write to the image this way
red = intensity.val[2];
edit flag offensive delete link more
0

answered 2014-01-23 07:25:08 -0600

This is possibly what you can do:

// This is pseudo-code, ofcourse you will need to change variable names with actual values or existing variables in your setup
// Region of interest - created manually or retrieved from somewhere
Rect bounding_box(x, y, width, heigth);
// Make a local copy of the region - do this to ensure you do not change the original image yet
Mat sub_region = original_image(bounding_box).clone();
// Perform something on this subregion --> for example apply Gaussian filter
GaussianBlur(sub_region, sub_region, Size(5,5), 0.0, 0.0, BORDER_DEFAULT );
// Copy the result back if you want it in your original image
sub_region.copyTo( original_image(bounding_box) );
edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-01-18 02:09:06 -0600

Seen: 528 times

Last updated: Jan 24 '14