Skip to content

Instantly share code, notes, and snippets.

@pranav083
Created November 24, 2018 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pranav083/9d3ad47a144157f4a7adfc1e9847b4b8 to your computer and use it in GitHub Desktop.
Save pranav083/9d3ad47a144157f4a7adfc1e9847b4b8 to your computer and use it in GitHub Desktop.
red color tuning
#######################################
###import function
import numpy as np
import cv2
#######################################
# Sample Pixel --> A - [213,154,150] , B - [113,154,150]
param_dict=[]
##param1=[0,230,230] ##red
##param2=[0,255,255]
#param1=[90,140,140] ##blue
#param2=[200,180,255]
param1=[0,230,200] ##reddish
param2=[0,255,255]
#######################################
###read the image
img =cv2.imread('Image2.jpg')
height, width, channels = img.shape
res = cv2.resize(img,(width//2, height//2), interpolation = cv2.INTER_CUBIC)
#######################################
#######################################
###Do the processing
### hsv image tranform
hsv = cv2.cvtColor(res,cv2.COLOR_BGR2HSV)
## Applying dilation on the image
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2,2))
dilation = cv2.dilate(hsv, kernel, iterations = 1)
### Applying erosion on the image
erosion = cv2.erode(hsv,kernel,iterations = 2)
## closing on the hsv image
closing =cv2.erode(dilation,kernel,iterations=1)
## opening on the hsv image
opening =cv2.dilate(erosion,kernel,iterations=1)
## Convert the parameters into a form that OpenCV can understand
lower = np.array(param1)
upper = np.array(param2)
mask = cv2.inRange(erosion, lower, upper)
ret = cv2.bitwise_and(res, res, mask= mask)
## applying blur operation on image
blur1 = cv2.GaussianBlur(erosion,(1,1),3)
blur2 = cv2.blur(erosion,(1,1))
blur3 = cv2.medianBlur(erosion,3)
## doing the grayscale conversion
gray = cv2.cvtColor(erosion,cv2.COLOR_BGR2GRAY)
__,thresh = cv2.threshold(gray,127,255,0)
## finding the contour of the image
__,contours,__= cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(res,contours,-1,(0,255,0),3)
#######################################
#######################################
### show the image
#cv2.imshow('resize_image',res)
#cv2.imshow('hsv_image',hsv)
#cv2.imshow('thresh',thresh)
#cv2.imshow('blur',blur2)
############
#cv2.imshow('dilation',dilation)
#cv2.imshow('erosion',erosion)
#cv2.imshow('closing',closing)
#cv2.imshow('opening',opening)
############
cv2.imshow('filter_image',ret)
######################################
#######################################
#######################################
### close and exit
cv2.waitKey()
cv2.destroyAllWindows()
#######################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment