opencv之resize

我在用tensorflow训练模型的时候,发现一个问题,由于训练的图片分辨率是固定的,但是我把训练好的模型拿来使用的时候,往往需要用来分类的图片分辨率不是我训练的分辨率。所以这个时候需要使用pnencv的resize来把图片重新缩放成模型的分辨率。

picpath = 'C:/Users/shenwei/Desktop/test/tt/0.png'
picsavepath = 'C:/Users/shenwei/Desktop/test/tt/12.png'
image = cv.imread(picpath,0)
cv.imshow("111",image)
cv.waitKey(0)
image = cv.resize(image,(28,28),interpolation =  cv.INTER_NEAREST)
image = 255 - image
ret,im_fixed=cv.threshold(image,100,255,cv.THRESH_BINARY)
cv.imshow("111",im_fixed)
cv.imwrite(picsavepath,im_fixed)
cv.waitKey(0)

缩放前的图像:

缩放后的图片是分辨率为28*28

 这里做了一下处理,把黑白图像进行了颠倒,另外通过cv.threshold函数对灰度图像进行二值化处理。

使得图片的黑白更加分明

参考下面博客:

https://blog.csdn.net/JNingWei/article/details/78218837

https://blog.csdn.net/yawdd/article/details/80180848

*******************增加一个批处理的例子**************

import cv2 as cv
import os

picpath = 'C:/Users/shenwei/Desktop/videoquality/black/'
picsavepath = 'C:/Users/shenwei/Desktop/videoquality/blackstandard/'

for root, dirs, files in os.walk(picpath): 
    i =0
    for file in files:
        i = i+1
        picpp = root +file
        image = cv.imread(picpp)
        image = cv.resize(image,(480,270),interpolation =  cv.INTER_NEAREST)
        newpath = picsavepath+'%d.jpg' % i
        cv.imwrite(newpath,image)
        print(newpath)

猜你喜欢

转载自blog.csdn.net/g0415shenw/article/details/87356832