# How to access specific pixel of 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. 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 = Point(0, 0);
corners = Point(my_img.width, 0);
corners = Point(0, my_img.height);
corners = Point(my_img.width, my_img.height);


then find its center

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


double radius = 2.0;


and then put new values inside of radius or circle

image.at<cv::Vec3b>(row,col) = newval;  //B
image.at<cv::Vec3b>(row,col) = newval;  //G
image.at<cv::Vec3b>(row,col) = newval;  //R


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

edit retag close merge delete

Sort by » oldest newest most voted 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
else
// Point is outside circle
}

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

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.

Official site

GitHub

Wiki

Documentation