python
import cv2 as cv
import numpy as np
import sys
sys.path.append("../")
import my_module as m
def function():
img = m.read("test.jpg")
cv.imshow("input", img)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = np.zeros(gray.shape, dtype = np.float32)
cv.normalize(gray, dst = dst, alpha = 0, beta = 1.0, norm_type = cv.NORM_MINMAX)
print(dst)
cv.imshow("min_max", np.uint8(dst * 255))
dst = np.zeros(gray.shape, dtype = np.float32)
cv.normalize(gray, dst = dst, alpha = 1.0, beta = 0, norm_type = cv.NORM_INF)
print(dst)
cv.imshow("INF", np.uint8(dst * 255))
dst = np.zeros(gray.shape, dtype = np.float32)
cv.normalize(gray, dst = dst, alpha = 1.0, beta = 0, norm_type = cv.NORM_L1)
print(dst)
cv.imshow("L1", np.uint8(dst * 10000000))
dst = np.zeros(gray.shape, dtype = np.float32)
cv.normalize(gray, dst = dst, alpha = 1.0, beta = 0, norm_type = cv.NORM_L2)
print(dst)
cv.imshow("L2", np.uint8(dst * 10000))
function()
cv.waitKey(0)
cv.destroyAllWindows()
python中的新知识
- np.float32()
- np.uint8
- cv.NORM_MINMAX
- cv.NORM_INF
- cv.NORM_L1
- cv.NORM_L2
- cv.normalize()
从上面程序看出,python中正则化时要将Mat中的数据转化为np.float32,处理完之后,显示之前需要再转化为np.uint8
c++
#include "all.h"
using namespace std;
using namespace cv;
void MyClass::day011() {
Mat img = read(PATH + "images\\test.jpg");
Mat gray;
if (img.empty()) {
printf("can't open image\n");
return;
}
cvtColor(img, gray, COLOR_BGR2GRAY);
gray.convertTo(gray, CV_32F);
Mat dst = Mat::zeros(gray.size(), CV_32FC1);
normalize(gray, dst, 1.0, 0, NORM_MINMAX);
dst = dst * 255;
dst.convertTo(dst, CV_8UC1);
imshow("minMax", dst);
dst = Mat::zeros(gray.size(), CV_32FC1);
normalize(gray, dst, 1.0, 0, NORM_INF);
dst = dst * 255;
dst.convertTo(dst, CV_8UC1);
imshow("INF", dst);
dst = Mat::zeros(gray.size(), CV_32FC1);
normalize(gray, dst, 1.0, 0, NORM_L1);
dst = dst * 10000000;
dst.convertTo(dst, CV_8UC1);
imshow("L1", dst);
dst = Mat::zeros(gray.size(), CV_32FC1);
normalize(gray, dst, 1.0, 0, NORM_L2);
dst = dst * 10000;
dst.convertTo(dst, CV_8UC1);
imshow("L2", dst);
waitKey(0);
}
c++中的新知识
- 新数据类型:CV_32FC1
- NORM_MINMAC
- NORM_INF
- NORM_L1
- NORM_L2
- normalize()
- Mat::convertTo()
从上面程序看出,c++中正则化时要将Mat中的数据转化为CV_32F,处理完之后,显示之前需要再转化为CV_8U