छवि विश्लेषण के उद्देश्य से हम Opencv (ओपन सोर्स कंप्यूटर विज़न लाइब्रेरी) पायथन लाइब्रेरी का उपयोग करते हैं। लाइब्रेरी का नाम जिसे opencv इंस्टाल करने के बाद इम्पोर्ट करना होता है cv2 है।
नीचे दिए गए उदाहरण में हम एक छवि फ़ाइलों में मौजूद आकृति पाते हैं। आकृति हमें छवि में मौजूद आकृतियों की पहचान करने में मदद करती है। समोच्च रेखा को एक समान तीव्रता वाली छवि की सीमा के साथ सभी बिंदुओं को मिलाने वाली रेखा के रूप में परिभाषित किया गया है। OPenCV में findContours फ़ंक्शन हमें आकृति की पहचान करने में मदद करता है। इसी तरह drawContours फंक्शन हमें कंट्रोवर्सी बनाने में मदद करता है। नीचे उन दोनों का सिंटैक्स दिया गया है।
सिंटैक्स
cv.FindContours(image, mode=CV_RETR_LIST, method=CV_CHAIN_APPROX_SIMPLE) Where image is the name of the image Mode is Contour retrieval mode Method is Contour approximation method cv.DrawContours(img, contours, contourIdx, colour, thickness) Where image is the name of the image contours – All the input contours. contourIdx – Parameter indicating a contour to draw. If it is negative, all the contours are drawn. color – Color of the contours thickness is how thick are the lines drawing the contour
उदाहरण
नीचे दिए गए उदाहरण में हम नीचे दी गई छवि को अपनी इनपुट छवि के रूप में उपयोग करते हैं। फिर इसके चारों ओर आकृति प्राप्त करने के लिए नीचे दिया गया प्रोग्राम चलाएं।

हम उपरोक्त आरेख में तीन आकृतियाँ पा सकते हैं। हम नीचे दिए गए प्रोग्राम का उपयोग करके सभी या उनमें से कुछ के चारों ओर आकृति बना सकते हैं।
उदाहरण
import cv2
# Load an image
image = cv2.imread(“path to image file”)
# Changing the colour-space
LUV = cv2.cvtColor(image, cv2.COLOR_BGR2LUV)
# Find edges
edges = cv2.Canny(LUV, 10, 100)
# Find Contours
contours, hierarchy = cv2.findContours(edges,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# Find Number of contours
print("Number of Contours is: " + str(len(contours)))
# Draw yellow border around two contours
cv2.drawContours(image, contours, 0, (0, 230, 255), 6)
cv2.drawContours(image, contours, 2, (0, 230, 255), 6)
# Show the image with contours
cv2.imshow('Contours', image)
cv2.waitKey(0) उपरोक्त कोड को चलाने से हमें निम्नलिखित परिणाम मिलते हैं -
आउटपुट
Number of Contours found = 3
और हमें आउटपुट दिखाते हुए नीचे दिया गया चित्र मिलता है।
