۱ـ بوسیله pointer ها
cv::Mat image=imread(loc);
int nl= image.rows; // number of lines
int nc= image.cols * image.channels();// total number of elements per line
for (int j=0; j<nl; j++)
{
uchar* data= image.ptr<uchar>(j);// get the address of row j
for (int i=0; i<nc; i++)
{
// process each pixel ---------------------
//example :
data[i]=23;
}
}
۲ـ با استفاده از iterator ها
auto it=image.begin<cv::Vec3b>();// obtain iterator at initial position
auto itend=image.end<cv::Vec3b>();// obtain end position
// loop over all pixels
for ( ; it!= itend; ++it)
{
// process each pixel ---------------------
//example :
(*it)[0]= 44;
(*it)[1]= 120;
(*it)[2]= 12;
}
3_استفاده از image.at
for(int i=0; i<img.rows; i++)
{
for(int j=0; i<img.cols; j++)
{
// process each pixel --------------------
//3 channel image
img.at<cv::Vec3b>(i,j)[0]=2;
img.at<cv::Vec3b>(i,j)[1]=23;
img.at<cv::Vec3b>(i,j)[2]=33;
//1 channel image
img.at<uchar>(i,j)=12;
}
}
روش های بالا رو بر اساس سرعت هر کدوم قرار دادم استفاده از pointer ها از همه سریع تره . و استفاده از iterator از همه بی خطر تر .
و این که سعی کنید از روش سوم استفاده نکنید .
به خاطر سرعت پایین image.at فقط زمانی که نیاز به دسترسی تصادفی به پیکسل ها دارید ازش استفاده کنید .