博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于OpenCV 将图片进行预处理,转变为MNIST图片格式
阅读量:2109 次
发布时间:2019-04-29

本文共 9614 字,大约阅读时间需要 32 分钟。

具体代码如下:

import cv2global imgglobal point1, point2def on_mouse(event, x, y, flags, param):    global img, point1, point2    img2 = img.copy()    if event == cv2.EVENT_LBUTTONDOWN:         #左键点击        point1 = (x,y)        cv2.circle(img2, point1, 10, (0,255,0), 5)        cv2.imshow('image', img2)    elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON):   #按住左键拖曳        cv2.rectangle(img2, point1, (x,y), (255,0,0), 5) # 图像,矩形顶点,相对顶点,颜色,粗细        cv2.imshow('image', img2)    elif event == cv2.EVENT_LBUTTONUP:         #左键释放        point2 = (x,y)        cv2.rectangle(img2, point1, point2, (0,0,255), 5)        cv2.imshow('image', img2)        min_x = min(point1[0], point2[0])        min_y = min(point1[1], point2[1])        width = abs(point1[0] - point2[0])        height = abs(point1[1] -point2[1])        cut_img = img[min_y:min_y+height, min_x:min_x+width]        resize_img = cv2.resize(cut_img, (28,28)) # 调整图像尺寸为28*28        ret, thresh_img = cv2.threshold(resize_img,127,255,cv2.THRESH_BINARY) # 二值化        cv2.imshow('result', thresh_img)        cv2.imwrite('D:\\deng\\ppp\\888.png', thresh_img)  # 预处理后图像保存位置def main():    global img    img = cv2.imread('D:\\deng\\ppp\\8.jpg')     # 手写数字图像所在位置    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)   # 转换图像为单通道(灰度图)    cv2.namedWindow('image')    cv2.setMouseCallback('image', on_mouse)      # 调用回调函数    cv2.imshow('image', img)    cv2.waitKey(0)if __name__ == '__main__':    main()

2.0版本

import cv2global imgglobal point1, point2cap = cv2.VideoCapture(0)def on_mouse(event, x, y, flags, param):    global img, point1, point2    img2 = img.copy()    if event == cv2.EVENT_LBUTTONDOWN:         #左键点击        point1 = (x,y)        cv2.circle(img2, point1, 10, (0,255,0), 5)        cv2.imshow('image', img2)    elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON):   #按住左键拖曳        cv2.rectangle(img2, point1, (x,y), (255,0,0), 5) # 图像,矩形顶点,相对顶点,颜色,粗细        cv2.imshow('image', img2)    elif event == cv2.EVENT_LBUTTONUP:         #左键释放        point2 = (x,y)        cv2.rectangle(img2, point1, point2, (0,0,255), 5)        cv2.imshow('image', img2)        min_x = min(point1[0], point2[0])        min_y = min(point1[1], point2[1])        width = abs(point1[0] - point2[0])        height = abs(point1[1] -point2[1])        cut_img = img[min_y:min_y+height, min_x:min_x+width]        resize_img = cv2.resize(cut_img, (28,28)) # 调整图像尺寸为28*28        ret, thresh_img = cv2.threshold(resize_img,127,255,cv2.THRESH_BINARY) # 二值化        cv2.imshow('result', thresh_img)        cv2.imwrite('D:\\deng\\ppp\\888.png', thresh_img)  # 预处理后图像保存位置def main():    global img    while True:        ret, frame = cap.read()        img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)        cv2.namedWindow('image')        cv2.setMouseCallback('image', on_mouse)  # 调用回调函数        cv2.imshow('image', img)        if cv2.waitKey(1) & 0xFF == ord('q'):            #cv2.waitKey(0)            cv2.destroyAllWindows()            break    #img = cv2.imread('D:\\deng\\ppp\\8.jpg')     # 手写数字图像所在位置    #img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)   # 转换图像为单通道(灰度图)if __name__ == '__main__':    main()

3.0版本 顺便调用tensorflow的模型进行数字识别。下述代码中,tensorflow_test是自己写的.py文件,并不是一个库。文件中的代码就是中所展示的代码。可以创建一个python文件,将代码拷贝进去。然后通过这个文件进行import。就可以使用import文件中的函数。

from scipy.spatial import distance as distfrom imutils import perspectivefrom imutils import contoursimport numpy as npimport imutilsimport cv2import tensorflow_testfrom PIL import Imagedef midpoint(ptA, ptB):    return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)#cap = cv2.VideoCapture("http://192.168.1.1:8080/?action=stream")cap = cv2.VideoCapture(0)#HSV颜色空间的红色区间Redlower=np.array([35, 43, 46])Redupper=np.array([124, 255, 255])while(True):    # Capture frame-by-frame    ret, frame = cap.read()    frame = imutils.resize(frame, width=600)    # Our operations on the frame come here    blurred = cv2.GaussianBlur(frame, (7, 7), 0)    hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)    #hsv = cv2.GaussianBlur(hsv, (7, 7), 0)    # perform edge detection, then perform a dilation + erosion to    # close gaps in between object edges    #edged = cv2.Canny(hsv, 50, 100)    #inRange()函数用于图像颜色分割    edged = cv2.inRange(hsv,Redlower,Redupper)    edged = cv2.dilate(edged, None, iterations=2)    edged = cv2.erode(edged, None, iterations=2)    #在OpenCV中,可以用findContours()函数从二值图像中查找轮廓。    #可以使用compare(),inrange(),threshold(),adaptivethreshold(),canny()等函数由灰度图或色彩图创建二进制图像。    #RETR_EXTERNAL表示只检测最外层轮廓。CHAIN_APPROX_SIMPLE表示压缩水平方向,垂直方向,对角线方向的元素,只保留    #该方向的终点坐标,例如一个矩形轮廓只需4个点来保存轮廓信息。    cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,                            cv2.CHAIN_APPROX_SIMPLE)[-2]    #cnts, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)    #cnts = imutils.grab_contours(cnts)    # sort the contours from left-to-right and initialize the    # 'pixels per metric' calibration variable    #(cnts, _) = contours.sort_contours(cnts)    pixelsPerMetric = None    # loop over the contours individually    for c in cnts:        # if the contour is not sufficiently large, ignore it        if cv2.contourArea(c) < 100:            continue        # compute the rotated bounding box of the contour        orig = frame.copy()        box = cv2.minAreaRect(c)        #box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)        box = cv2.boxPoints(box)        box = np.array(box, dtype="int")        # order the points in the contour such that they appear        # in top-left, top-right, bottom-right, and bottom-left        # order, then draw the outline of the rotated bounding        # box        box = perspective.order_points(box)        #绘制轮廓drawContours()函数        #第二个参数表示所有输入轮廓,每一个轮廓存储为一个点向量,即point类型的vector表示        #第三个参数为负数,则绘制所有轮廓        #第四个参数为轮廓颜色        #第五个参数为轮廓线条的粗细度        cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)        # loop over the original points and draw them        for (x, y) in box:            cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)            # unpack the ordered bounding box, then compute the midpoint            # between the top-left and top-right coordinates, followed by            # the midpoint between bottom-left and bottom-right coordinates            (tl, tr, br, bl) = box            #print(box)            (tltrX, tltrY) = midpoint(tl, tr)            (blbrX, blbrY) = midpoint(bl, br)            # compute the midpoint between the top-left and top-right points,            # followed by the midpoint between the top-righ and bottom-right            (tlblX, tlblY) = midpoint(tl, bl)            (trbrX, trbrY) = midpoint(tr, br)            # draw the midpoints on the image            cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)            cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)            cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)            cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)            # draw lines between the midpoints            cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),                     (255, 0, 255), 2)            cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),                     (255, 0, 255), 2)            # compute the Euclidean distance between the midpoints            dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))            dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))            # if the pixels per metric has not been initialized, then            # compute it as the ratio of pixels to supplied metric            # (in this case, inches)            if pixelsPerMetric is None:                # pixelsPerMetric = dB / args["width"]                pixelsPerMetric = dB / 20.5                # compute the size of the object                dimA = dA / pixelsPerMetric                dimB = dB / pixelsPerMetric                D = 921.43 / pixelsPerMetric                # draw the object sizes on the image                cv2.putText(orig, "{:.1f}mm".format(dimA),                            (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX,                            0.65, (255, 255, 255), 2)                cv2.putText(orig, "{:.1f}mm".format(dimB),                            (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,                            0.65, (255, 255, 255), 2)                cv2.putText(orig, "{:.1f}mm".format(D),                            (int(trbrX + 20), int(trbrY+20)), cv2.FONT_HERSHEY_SIMPLEX,                            0.65, (255, 255, 255), 2)                # show the output image                cut_img = orig[int(tl[1]): int(bl[1]), int(tl[0]): int(tr[0])]                resize_img = cv2.resize(cut_img, (28, 28))  # 调整图像尺寸为28*28                resize_img = cv2.cvtColor(resize_img, cv2.COLOR_RGB2GRAY)                ret, thresh_img = cv2.threshold(resize_img, 127, 255, cv2.THRESH_BINARY)  # 二值化                cv2.imwrite('D:\\deng\\ppp\\888.png', thresh_img)                im = Image.open('D:\\deng\\ppp\\888.png')                #im = cv2.imread('D:\\deng\\ppp\\888.png')                #im_gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)                data = list(im.getdata())                result = [(255 - x) * 1.0 / 255.0 for x in data]                tensorflow_test.prediction_num(result)                cv2.imshow('result', thresh_img)                cv2.imshow("Image", orig)                if cv2.waitKey(1) & 0xFF == ord('q'):                    cap.release()                    cv2.destroyAllWindows()                    break

转载地址:http://esfef.baihongyu.com/

你可能感兴趣的文章
【手机自动化测试】monkey测试
查看>>
【英语】软件开发常用英语词汇
查看>>
Fiddler 抓包工具总结
查看>>
【雅思】雅思需要购买和准备的学习资料
查看>>
【雅思】雅思写作作业(1)
查看>>
【雅思】【大作文】【审题作业】关于同不同意的审题作业(重点)
查看>>
【Loadrunner】通过loadrunner录制时候有事件但是白页无法出来登录页怎么办?
查看>>
【English】【托业】【四六级】写译高频词汇
查看>>
【托业】【新东方全真模拟】01~02-----P5~6
查看>>
【托业】【新东方全真模拟】03~04-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST05~06-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST09~10-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST07~08-----P5~6
查看>>
solver及其配置
查看>>
JAVA多线程之volatile 与 synchronized 的比较
查看>>
Java集合框架知识梳理
查看>>
笔试题(一)—— java基础
查看>>
Redis学习笔记(二)— 在linux下搭建redis服务器
查看>>
Redis学习笔记(三)—— 使用redis客户端连接windows和linux下的redis并解决无法连接redis的问题
查看>>
Intellij IDEA使用(一)—— 安装Intellij IDEA(ideaIU-2017.2.3)并完成Intellij IDEA的简单配置
查看>>