mardi 24 février 2015

OpenCV: detect 4 interesting pixels

I read this picture using OpenCV:


enter image description here


I detect the boundaries of the objects inside it and display the picture:


enter image description here


(sorry for not having installed a tool yet to cut the picture I want to display exactly).


Here is my code:



import cv2
import numpy as np
from matplotlib import pyplot as plt


im=cv2.imread('db/4.jpg')
#mask=np.zeros(img.shape[:2],np.uint8)
imgray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh=cv2.threshold(imgray,242,245,235)
contours,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

cv2.drawContours(im,contours,-1,(0,255,0),3)
cv2.imshow("GL",im)
cv2.waitKey(0)
cv2.destroyAllWindows()


What I want to do:


I want to have the coordinates of 4 pixels belonging to one of the contours and:



  • First one is the closet from the X axis

  • Second one is the farest from the X axis

  • Third one is the closet from the Y axis

  • Fourth one is the farest from the Y axis


Note that the number of contours variable in my code can vary depending on the picture I read.


Aucun commentaire:

Enregistrer un commentaire