像素值统计
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()
#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;
}