ইমেজ এনালাইসিসের উদ্দেশ্যে আমরা OpenCV (Open Source Computer Vision Library) পাইথন লাইব্রেরি ব্যবহার করি। 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
# একটি ইমেজ লোড
image = cv2.imread("path to image file")
# কালার স্পেস পরিবর্তন
LUV = cv2.cvtColor(image, cv2.COLOR_BGR2LUV)
# এজ খুঁজুন
edges = cv2.Canny(LUV, 10, 100)
# কন্টুর খুঁজুন
contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# কন্টুরের সংখ্যা বের করুন
print("Number of Contours is: " + str(len(contours)))
# দুটি কন্টুরের চারপাশে হলুদ বর্ডার আঁকুন
cv2.drawContours(image, contours, 0, (0, 230, 255), 6)
cv2.drawContours(image, contours, 2, (0, 230, 255), 6)
# কন্টুরসহ ইমেজ দেখান
cv2.imshow('Contours', image)
cv2.waitKey(0)
উপরের কোড রান করলে নিচের ফলাফল পাওয়া যায়:
আউটপুট
Number of Contours found = 3
এবং নিচের ডায়াগ্রাম আউটপুট হিসেবে দেখানো হয়েছে।
