Ask Your Question

How to access specific pixel of image

asked 2014-05-06 15:56:33 -0500

FLY gravatar image

How can i access only the pixels of specific part of the image ? Is there any proper method of accessing those or i need to use hit and trial method for it . For example i want to access all the pixels within the circle ( this circle is just draw for understanding) will not have it in real problem.

image description

I want to access only these pixels for changing values , one way which just come to mind is

getting the four corners of image

vector<Point> corners(4);
corners[0] = Point(0, 0);
corners[1] = Point(my_img.width, 0);
corners[2] = Point(0, my_img.height);
corners[3] = Point(my_img.width, my_img.height);

then find its center

Point center = Point(my_img.size().width/2, my_img.size().height/2);

make radius

double radius = 2.0;

and then put new values inside of radius or circle<cv::Vec3b>(row,col)[0] = newval[0];  //B<cv::Vec3b>(row,col)[1] = newval[1];  //G<cv::Vec3b>(row,col)[2] = newval[2];  //R

but then how to access pixels inside it , or any other logic or idea is appriciated

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-05-06 17:52:03 -0500

unxnut gravatar image

Compute the distance of each point from the center. If it is less than radius, the point is inside the circle. Here is the code:

for ( int r = 0; r < image.rows; r++ )
    for ( int c = 0; c < image.cols; c++ )
        double dist = sqrt ( ( c - center.x ) * ( c - center.x ) + ( r - center.y ) * ( r  - center.y ));
        if ( dist < radius )
            // Point is inside circle
            // Point is outside circle
edit flag offensive delete link more


it come to my mind too , nice answer but radius is the constant value like 2 , how i can compare the pixel values with it , like may be pixel(0,0) have greater value then radius ? because i want to change the color values in that area , thanks

FLY gravatar imageFLY ( 2014-05-07 03:30:09 -0500 )edit

When the point is inside circle, all you have to do is use the at member like you suggested using (r,c) in place of (row,column) in your code.

unxnut gravatar imageunxnut ( 2014-05-07 07:04:28 -0500 )edit

Question Tools


Asked: 2014-05-06 15:56:33 -0500

Seen: 927 times

Last updated: May 06 '14