一、图像像素读写-基于数据下表
using namespace std;
using namespace cv;
class QuickDemo
{
public:
void pixel_visit_Demo(Mat& image)
{
int width = image.cols;
int height = image.rows;
int channel = image.channels();
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
if (channel == 1)
{
int pv = image.at<uchar>(row, col);
image.at<uchar>(row, col) = 255 - pv;
}
else if(channel == 3)
{
Vec3b bjr = image.at<Vec3b>(row, col);
image.at<Vec3b>(row, col)[0] = 255 - bjr[0];
image.at<Vec3b>(row, col)[1] = 255 - bjr[1];
image.at<Vec3b>(row, col)[2] = 255 - bjr[2];
}
}
}
imshow("pixel-change-show", image);
waitKey(0);
}
};
int main()
{
Mat src = imread("data/right.bmp");
if (src.empty())
{
cout << "load image error !" << endl;
}
QuickDemo qd;
qd.pixel_visit_Demo(src);
}
一、图像像素读写-基于指针
void pixel_visit_Demo_ptr(Mat& image)
{
int width = image.cols;
int height = image.rows;
int channel = image.channels();
for (int row = 0; row < height; row++)
{
uchar* current_row = image.ptr<uchar>(row);
for (int col = 0; col < width; col++)
{
if (channel == 1)
{
int pv = *current_row;
image.at<uchar>(row, col) = 255 - pv;
}
else if (channel == 3)
{
* current_row++ = 255 - *current_row;
* current_row++ = 255 - *current_row;
* current_row++ = 255 - *current_row;
}
}
}
imshow("pixel-change-show", image);
waitKey(0);
}