การพัฒนาโปรแกรมนับวัตถุในภาพ
การพัฒนาโปรแกรมตรวจนับวัตถุในภาพ (Count Object) ซึ่งมีขั้นตอนดังนี้ คลิกเพื่อโหลดภาพ
👉การเปิดข้อมูล (Read Data)
import cv2
import numpy as np
# Load the image
image_path = r"C:\Users\user\Desktop\777.JPG"
image = cv2.imread(image_path)
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply thresholding to create a binary image
_, thresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV)
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Count objects
object_count = len(contours)
print(f"Number of objects detected: {object_count}")
# Draw contours
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
# Display result
cv2.imshow("Detected Objects", image)
cv2.imshow("gray", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
เมื่อรันโปรแกรมแล้วผลลัพท์ที่ได้
👉การเปิดข้อมูล (Read Data)
import cv2
import numpy as np
# Load the image
image_path = r"C:\Users\user\Desktop\33.JPG"
image = cv2.imread(image_path)
# Convert image to HSV color space
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Define lower and upper bounds for the green background
lower_green = np.array([35, 40, 40])
upper_green = np.array([85, 255, 255])
# Create mask for the green background
mask = cv2.inRange(hsv, lower_green, upper_green)
# Invert mask to keep only the white objects
mask_inv = cv2.bitwise_not(mask)
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply the mask to the grayscale image
masked_gray = cv2.bitwise_and(gray, gray, mask=mask_inv)
# Apply thresholding to create a binary image
_, thresh = cv2.threshold(masked_gray, 200, 255, cv2.THRESH_BINARY)
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Count objects
object_count = len(contours)
print(f"Number of objects detected: {object_count}")
# Draw contours
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
# Display result
cv2.imshow("Detected Objects", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
เมื่อรันโปรแกรมแล้วผลลัพท์ที่ได้
👉การเปิดข้อมูล (Read Data)
import cv2
import numpy as np
# Load the image
image_path = r"C:\Users\user\Desktop\color.JPG"
image = cv2.imread(image_path)
# Convert to HSV color space
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Define the lower and upper bounds for black color in HSV
lower_black = np.array([0, 0, 0]) # Lower bound for black
upper_black = np.array([180, 255, 50]) # Upper bound for black
# Create mask for black color (binary image)
black_mask = cv2.inRange(hsv, lower_black, upper_black)
# Invert the mask so that black objects appear black (0) and the background is white (255)
binary_result = 255 - black_mask
# Apply thresholding to create a binary image (if needed)
_, thresh = cv2.threshold(binary_result, 100, 255, cv2.THRESH_BINARY_INV)
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Count objects
object_count = len(contours)
print(f"Number of objects detected: {object_count}")
# Convert binary image to 3-channel BGR for drawing colored contours
binary_result_bgr = cv2.cvtColor(binary_result, cv2.COLOR_GRAY2BGR)
# Draw contours on the binary image
cv2.drawContours(binary_result_bgr, contours, -1, (0, 255, 0), 2) # Draw contours in green
# Display results
cv2.imshow("Binary Mask with Contours", binary_result_bgr)
cv2.imshow("Detected Objects on Original Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
เมื่อรันโปรแกรมแล้วผลลัพท์ที่ได้