C++: void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR )
//0.5 0.5的比例缩放;
cv::resize(img, dst,cv::Size(0,0),(0.5),(0.5),1);
//将图像尺寸变为512,512
cv::resize(img, dst,cv::Size(512,512));
Python: cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst
#按比例缩放
dst=cv2.resize(src,(0,0),fx=1/2,fy=1/2,interpolation=cv2.INTER_LINEAR)
#将源图像的大小变为512*512
dst=cv2.resize(src,(512,512))
interpolation –
interpolation method:
当压缩一幅图像时,选择INTER_AREA算法实现效果较好;当放大一幅图像时,常常选择INTER_CUBIC,但是速度较慢,INTER_LINEAR响应较快,效果也相对较好。
C++: Mat imread(const string& filename, int flags=1 )
Python: cv2.imread(filename[, flags]) → retval
flags –
Flags specifying the color type of a loaded image:
at()函数遍历
uchar、Vec3b表示图像元素的类型.
void traverse_at(Mat img)
{
int row = img.rows;
int col = img.cols;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
img.at(i, j)[0] = 255 - img.at(i, j)[0];//B通道
img.at(i, j)[1] = 255 - img.at(i, j)[1];//G通道
img.at(i, j)[2] = 255 - img.at(i, j)[2];//R通道
}
}
imshow("reverse", img);
waitKey(0);
destroyAllWindows();
}
step:行数*通道数,
指针方式遍历:
void traverse_pt(Mat img)
{
int row = img.rows;
int step = img.step;
uchar* pImg = img.data;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < step; j++)
pImg[j] = 255 - pImg[j];
pImg += img.step;
}
imshow("reverse——pt", img);
waitKey(0);
destroyAllWindows();
}