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_NEAREST - a nearest-neighbor interpolation
- INTER_LINEAR - a bilinear interpolation (used by default)
- INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.
- INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood
- INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood
当压缩一幅图像时,选择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:
- CV_LOAD_IMAGE_ANYDEPTH = 2 - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
- CV_LOAD_IMAGE_ANYCOLOR =4
- CV_LOAD_IMAGE_COLOR = 1 - If set, always convert image to the color one
- CV_LOAD_IMAGE_GRAYSCALE = 0 - If set, always convert image to the grayscale one
- CV_LOAD_IMAGE_UNCHANGED =-1
- >0 Return a 3-channel color image.
- =0 Return a grayscale image.
- <0 Return the loaded image as is (with alpha channel).
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<Vec3b>(i, j)[0] = 255 - img.at<Vec3b>(i, j)[0];//B通道
img.at<Vec3b>(i, j)[1] = 255 - img.at<Vec3b>(i, j)[1];//G通道
img.at<Vec3b>(i, j)[2] = 255 - img.at<Vec3b>(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();
}