Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # check other adjustments (contrast, luminance)
- # check if you can disable auto adjustments in camera
- # check for all these to be adaptively applied
- import cv2
- import numpy as np
- import math
- # get image
- img = cv2.imread('C:\\Users\\calin\\Desktop\\Bosch\\lateralControl\\sampleImage.jpg', -1)
- # scale down
- # check other dimensions
- # check other interpolation methods
- # check speed without resizing
- width = 700
- height = 400
- img = cv2.resize(img, (width, height), interpolation = cv2.INTER_AREA)
- # crop to region of interest
- # check other shapes for RoI
- """
- def cropRegion(img, vertices):
- mask = np.zeros_like(img)
- channelCount = img.shape[2]
- matchMaskColor = (255,) * channelCount
- cv2.fillPoly(mask, vertices, matchMaskColor)
- maskedImage = cv2.bitwise_and(img, mask)
- return maskedImage
- roiVertices = [
- (30, height - 30),
- (width / 2, height / 2),
- (width - 30, height - 30)
- ]
- img = cropRegion(img, np.array([roiVertices], np.int32),)
- img = img[(int(height/2)):(height - 30), 30:(width - 30)]
- """
- # grayscale
- # check performance without grayscale
- # check selecting a single channel (R, G, B) instead of grayscale
- img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- # otsu - works only without cropping
- # check different values of blur
- # check with/without blur
- # check with/without
- img = img[(int(height/1.8)):(height - 30), (int(width*0.2)):(width - (int(width*0.15)))]
- img = cv2.GaussianBlur(img, (5,1), 0)
- img1, img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
- # adaptive mean threshold
- # check different values
- #img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, -10)
- # adaptive gaussian threshold
- # check different values
- #img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, -15)
- # opening/closing (erosion + dilation)
- # check different values for kernel
- #kernel = np.ones((1,2), np.uint8)
- #img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
- #kernel = np.ones((3,3), np.uint8)
- #img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
- # canny
- #img = cv2.Canny(img, 100, 200)
- # crop
- def cropRegion(img, vertices):
- mask = np.zeros_like(img)
- channelCount = 1
- matchMaskColor = (255,) * channelCount
- cv2.fillPoly(mask, vertices, matchMaskColor)
- maskedImage = cv2.bitwise_and(img, mask)
- return maskedImage
- roiVertices = [
- (0, len(img)),
- (len(img[0]) / 2, 0),
- (len(img[0]), len(img))
- ]
- img = cropRegion(img, np.array([roiVertices], np.int32),)
- # custom method
- """
- m = len(img)
- n = len(img[0])
- bigSm = 0
- bigNm = 0
- for i in range(m):
- sm = 0
- nm = 0
- for j in range (n):
- if img[i][j] != 0 :
- sm = sm + j
- nm = nm + 1
- if nm != 0:
- aux = int(sm/nm)
- bigSm = bigSm + aux
- bigNm = bigNm + 1
- img[i][aux] = 255
- bigAux = int(bigSm/bigNm)
- for i in range(m):
- img[i][bigAux] = 255
- """
- cv2.imshow('image', img)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement