0

This is our image processing homework, I know that we have to use morphological methods,and some cv2 methods like threshold , and we have to work on it as a colorful picture cause in Gray scale we will lose some information which we need. We have to work on the attached photo input pic so we will have output photo this in output.

I'll appreciate any help

import cv2 
import numpy as np
import matplotlib.pyplot as plt
img=cv2.imread('shapes.jpg')
img= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
hist=cv2.calcHist(img,[0],None,[256],[0,256])
ret,img=cv2.threshold(img,110,255,cv2.THRESH_BINARY_INV)
#th=cv2.adaptiveThreshold(img,100,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,16,5)
se=np.uint8([[-1,0,1],[-1,0,1],[-1,0,1]])
se=np.ones(5*5)
se=cv2.getStructuringElement(cv2.MORPH_RECT,(15,5))
img=cv2.morphologyEx(img,cv2.MORPH_CLOSE,se)
img=cv2.erode(img,se)
#plt.plot(hist)
plt.gray()
plt.imshow(img)
plt.show()
  • Hello and welcome to StackOverflow! This site isn't meant for "How do I do this?" questions. If you've made an attempt and it's not working as intended, then we can help you with that. – Daniel Walker Jun 22 '20 at 20:34
  • @DanielWalker u know , i made an attempt for it in gray scale but i have no idea what should I do in color channels.I add the code I wrote and its not working actually. – Mehrafarin Rhm Jun 22 '20 at 20:43
  • You need to edit your post and show your code there. – Daniel Walker Jun 22 '20 at 20:44
  • 1
    I just did it. sorry. – Mehrafarin Rhm Jun 22 '20 at 20:46
  • Maybe have a read here... https://www.pyimagesearch.com/2016/02/08/opencv-shape-detection/ – Mark Setchell Jun 22 '20 at 21:29
  • 1
    Why do you compute the histogram and then not use it? Use cv2.inRange() to threshold on blue. Then get contours and fit them to polygon to select only 4 sided ones. Then draw filled contours for the blue, squares that you have now left after the color and shape filtering. – fmw42 Jun 22 '20 at 22:15

0 Answers0