import numpy as np import cv2 def ComputeMinLevel(hist, pnum): index = np.add.accumulate(hist) return np.argwhere(index>pnum * 8.3 * 0.01)[0][0] def ComputeMaxLevel(hist, pnum): hist_0 = hist[::-1] Iter_sum = np.add.accumulate(hist_0) index = np.argwhere(Iter_sum > (pnum * 2.2 * 0.01))[0][0] return 255-index def LinearMap(minlevel, maxlevel): if (minlevel >= maxlevel): return [] else: index = np.array(list(range(256))) screenNum = np.where(index<minlevel,0,index) screenNum = np.where(screenNum> maxlevel,255,screenNum) for i in range(len(screenNum)): if screenNum[i]> 0 and screenNum[i] < 255: screenNum[i] = (i - minlevel) / (maxlevel - minlevel) * 255 return screenNum def CreateNewImg(img): h, w, d = img.shape newimg = np.zeros([h, w, d]) for i in range(d): imghist = np.bincount(img[:, :, i].reshape(1, -1)[0]) minlevel = ComputeMinLevel(imghist, h * w) maxlevel = ComputeMaxLevel(imghist, h * w) screenNum = LinearMap(minlevel, maxlevel) if (screenNum.size == 0): continue for j in range(h): newimg[j, :, i] = screenNum[img[j, :, i]] return newimg if __name__ == '__main__': img = cv2.imread('D:\shujuji\wu\\Image_20230225221250865.jpg') newimg = CreateNewImg(img) cv2.imshow('original_img', img) cv2.imshow('new_img', newimg / 200) cv2.waitKey(0) cv2.destroyAllWindows()修改调整图片大小
时间: 2024-01-23 17:02:32 浏览: 127
import numpy as np import matplotlib.pyplot as plt import math
抱歉,根据您提供的代码,我无法直接回答您的问题。该代码是用Python和OpenCV库编写的图像处理程序,用于调整图像的亮度和对比度。如果您需要调整图片大小,可以使用OpenCV的resize()函数。例如,可以使用以下代码将图像大小调整为宽度为400像素,高度按比例缩放:
```python
resized_img = cv2.resize(img, (400, int(img.shape[0]*400/img.shape[1])))
```
其中,img是原始图像,resized_img是调整大小后的图像。您可以将其放置在CreateNewImg()函数后面,以获得调整大小后的图像。
阅读全文