def filter(x, locations): img = cv2.imread(os.path.join(locations, '%dSubtract.png' % (x))) ## imgs = cv2.imread('testSubtracted.png',0) imgs = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) blur = cv2.GaussianBlur(imgs, (17, 17), 0) ret, th = cv2.threshhold(blur, 100, 255, cv2.THRESH_TOZERO) cv2.imwrite(os.path.join(locations, '%dFiltered.png' % (x)), th) return th
def loopThresh(image, step, maxValue, nrow, nocols=maxValue // step): i = 1 for thresh in range(0, maxValue, step): th, dst = cv2.threshhold(image, thresh, maxValue, cv2.THRESH_BINARY) fig.add_subplot(nrow, ncols, i) plt.imshow(dst) i = i + 1
#APPLYING GAUSIAN BLUR GAUSSIAN BLUR SMOOTHENS THE IMAGE AND THUS REDUCES THE NOISE greyscale = cv2.GaussianBlur(greyscale, (21, 21), 0) # THE MOST IMPORTANT STEP SETTING THE FIRST FRAME if firstf is None: firstf = greyscale continue #FINDING THE DIFFERENCE B/W CURRENT FRAME AND FIRST FRAME delta = cv2.absdiff(firstf, greyscale) #FINDING THE THRESHHOLD FRAME AS WE CAN DRAW COUNTOURS ON THRESH FRAME ONLY #THE SECOND ARGUMENT IN THE THRESHHOLD FUNCTION IS THE DIFFERENCE BELOW WHICH WE WILL NOT CONSIDER MOTION #IT IS A HYPERPARAMETER AND CAN BE FINE TUNED AS PER ROOM CONDITIONS thresh = cv2.threshhold(delta, 30, 255, cv2.THRESH_BINARY)[1] thresh = cv2.dilate(thresh, None, iterations=2) #FINDING CONTOURS (_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #FINDING CONTOURS HAVING AREA GREATER THAN AREA 1000 AND MAKING RECTANGLE AROUND THEM for contour in cnts: if cv2.contourArea(contour) < 1000: continue [x, y, h, w] = cv2.boundingRect(contour) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3) print(frame)
宽度对应列数,高度对应行数 3旋转:cv2.getRotationMatrix2 4仿射变换:原图中所有的平行线在结果图像中同样平行,为了创建这个矩阵需要从原图像 中找到三个点以及他们在输出图像中的位置 cv2.getAffineTransform , cv2.warpAffine 5透视变换 视角变换,需要3*3变换矩阵,在变换前后直线还是直线,要构建这个矩阵,需要在输入图像 中找4个点,以及他们在输出图像上对应的位置,4个点中任意三个不能共线 变换矩阵可通过cv2.getPerspectiveTransform()构建 然后矩阵传递给cv2.warpPerspective ''' '' ''''' 图像阈值: 当像素高于阈值时,给这个像素赋予新值 cv2.threshhold(),第一个参数是原图像,原图像应该是灰度图,第二个参数就是用来对像素值 进行分类的阈值。第三个参数是像素高于(或小于)阈值时应被赋予的新像素值 自适应阈值: 根据图像上的每一个小区域计算与其对应的阈值 Adaptive Method Otsu's二值化 对一个双峰图像自动根据其直方图计算出其阈值 cv2.threshold(),多传入一个参数cv2.THRESH_OTSU,把阈值设为0 ''' '''''
"--image", required=True, help="path to input image to be OCR'd") ap.add_argument("-p", "--preprocess", type=str, default="thresh", help="type of preprocessing to be done") args = vars(ap.parse_args()) #Load the image and convert to greyscale image = cv2.imread(args["image"]) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if args["preprocess"] == "thresh": gray = cv2.threshhold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] elif args["preprocess"] == "blur": gray = cv2.medianBlur(gray, 3) #Lo anterior no jala y era para limpiar la imagen, esto es el puro modulo filename = "2018_11_11-LabDeMicroSubirBlackBoard.png".format(os.getpid()) cv2.imwrite(filename, gray) text = pytesseract.image_to_string(Image.open(filename)) os.remove(filename) print(text) #show the output images cv2.imshow("Image", image) cv2.imshow("Output", gray)
break # resixe frame, convert to grayscale, blur frame = imutils.resize(frame, width=500) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (21, 21), 0) # if first frame is none, make it gray if firstFrame is None: firstFrame = gray continue # compute absolute difference between first and current frame # first frame framedelta = cv2.absdiff(firstFrame, gray) thresh = cv2.threshhold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1] # idlate threshod image to fill holes # on thresholded image thresh = cv2.dilate(thresh, None, iterations=2) (cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # loop over the contours for c in cnts: # if the contour is small, igrore if cv2.contourArea(c) < args["min_area"]: continue # compute teh bounding box for teh contour # update th text
# -*- coding: utf-8 -*- """ Created on Tue Jun 19 08:52:46 2018 @author: ayush """ import cv2 as cv import numpy as np image = cv.imread('dog.jpg',0) cv.imshow('Original',image) #VALUES BELOW 127 GOES TO 0 ret,thresh1 = cv.threshhold(image,127,255,cv.THRESH_BINARY) cv.imshow('1threshold binary',thresh1) ret,thresh2 = cv.threshhold(image,127,255,cv.THRESH_BINARY_INV) cv.imshow('threshold binary',thresh2) ret,thresh3 = cv.threshhold(image,127,255,cv.THRESH_TRUNC) cv.imshow('Thresh trunc',thresh3) ret,thresh4 = cv.threshhold(image,127,255,cv.THRESH_TOZERO) cv.imshow('thresh to zero',thresh4) ret,thresh5 = cv.threshhold(image,127,255,cv.THRESH_TOZERO_INV) cv.imshow('thresh to hold zero inv',thresh5)