获取并修改像素值
C++
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int artc, char** argv) {
Mat src = imread("/home/kavin/Pictures/test.png", IMREAD_COLOR);
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
for (int i = 5; i < 50; i++) { // 行
for (int j = 30; j < 100; j++) { // 列
src.at<Vec3b>(i, j)[0] = 255;
src.at<Vec3b>(i, j)[1] = 0;
src.at<Vec3b>(i, j)[2] = 0;
}
}
namedWindow("input", WINDOW_AUTOSIZE);
imshow("input", src);
imwrite("pix.png", src);
waitKey(0);
// 对应的roi区域 (左上角坐标(w,h),width,height)
Rect rect_roi(30, 5, 70, 45);
Mat img_roi = src(rect_roi);
imshow("roi", img_roi);
return 0;
}
修改像素值
python
src = cv.imread("./test.png")
// 获取某个点的像素值
px = src[200, 250] # [ 27 15 244]
// 获取某个点某个通道的像素值
blue = src[200, 250, 0] # 27
// 修改高度为200的所有像素值为0
src[200, :] = [0, 0, 0]
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)
cv.waitKey(0)
cv.destroyAllWindows()
修改像素值
通道拆分与合并
b, g, r = cv.split(src)
img = cv.merge((b, g, r))
B通道图像
转换颜色空间
hsv = cv.cvtColor(src, cv.COLOR_BGR2HSV)
cv.namedWindow("hsv", cv.WINDOW_AUTOSIZE)
cv.imshow("hsv", hsv)
HSV颜色空间