Opencv-图像像素值统计

图像像素值统计

    • 知识点
    • python代码
    • c++代码

知识点

像素值统计

  • 最小(min)
  • 最大(max)
  • 均值(mean)
  • 标准方差(standard deviation)
    API知识点
  • 最大最小值minMaxLoc
  • 计算均值与标准方差meanStdDev

python代码

import cv2 as cv
import numpy as np

src = cv.imread("C:/Users/qqxd/Desktop/opencvcode/images/test.png", cv.IMREAD_GRAYSCALE)
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)

min, max, minLoc, maxLoc = cv.minMaxLoc(src)
print("min: %.2f, max: %.2f"% (min, max))
print("min loc: ", minLoc)
print("max loc: ", maxLoc)

means, stddev = cv.meanStdDev(src)
print("mean: %.2f, stddev: %.2f"% (means, stddev))
src[np.where(src < means)] = 0
src[np.where(src > means)] = 255
cv.imshow("binary", src)

cv.waitKey(0)
cv.destroyAllWindows()

c++代码

#include 
#include 

using namespace cv;
using namespace std;

int main(int argc, const char *argv[])
{
	Mat src = imread("C:/Users/qqxd/Desktop/opencvcode/images/test.png", IMREAD_GRAYSCALE);
	if (src.empty()) {
		printf("could not load image...\n");
		return -1;
	}
	namedWindow("input", WINDOW_AUTOSIZE);
	imshow("input", src);

	double minVal; double maxVal; Point minLoc; Point maxLoc;
	minMaxLoc(src, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
	printf("min: %.2f, max: %.2f \n", minVal, maxVal);
	printf("min loc: (%d, %d) \n", minLoc.x, minLoc.y);
	printf("max loc: (%d, %d)\n", maxLoc.x, maxLoc.y);

	// 彩色图像 三通道的 均值与方差
	src = imread("C:/Users/qqxd/Desktop/opencvcode/images/test.png");
	Mat means, stddev;
	meanStdDev(src, means, stddev);
	printf("blue channel->> mean: %.2f, stddev: %.2f\n", means.at<double>(0, 0), stddev.at<double>(0, 0));
	printf("green channel->> mean: %.2f, stddev: %.2f\n", means.at<double>(1, 0), stddev.at<double>(1, 0));
	printf("red channel->> mean: %.2f, stddev: %.2f\n", means.at<double>(2, 0), stddev.at<double>(2, 0));

	
	waitKey(0);
	return 0;
}

运行结果如下:
Opencv-图像像素值统计_第1张图片

你可能感兴趣的:(Opencv)