检测鱼类
颜色可以用不同的格式表示。有多种方式来表示颜色:
- RGB(红,绿,蓝)
- BGR(蓝,绿,红)
- HSV(色调,饱和度,值)
HSV 颜色空间
HSV代表色调、饱和度和值。这是一种常用于图像处理和计算机视觉任务的颜色空间表示。使用HSV颜色空间进行颜色选择的优势在于它允许轻松地操作色调、饱和度和值。然而,一个缺点是它可能无法准确表示所有颜色。如果你仔细观察这张图片,你会注意到你无法获得所有颜色:
如何使用颜色进行对象检测?
使用颜色进行对象检测涉及基于图像中对象的颜色属性来识别对象。有5个主要步骤:
- 选择颜色空间:通常,HSV是一个很好的选择。
- 阈值处理:在选定的颜色空间中设置阈值,以隔离与要检测的对象颜色匹配的图像区域。例如,如果你选择HSV颜色空间,定义色调、饱和度和值通道的范围。如果你想检测蓝色对象,你需要为蓝色定义特定的下限和上限。
- 生成掩码:创建一个二进制掩码,其中指定颜色范围内的像素设置为1(白色),范围外的像素设置为0(黑色)。这个掩码将分离图像中的感兴趣区域,在这种情况下,它将隔离所需的颜色。
- 轮廓检测:找到掩码后,找到轮廓就很简单了。OpenCV提供了cv2.findContours()函数用于查找轮廓。
- 绘制矩形:cv2.findContours()函数将返回一系列轮廓。遍历该列表,并使用cv2.boundingRect(contour)函数找到每个轮廓的边界矩形的坐标。之后,使用这些坐标绘制矩形。
检测蓝色球体
交互式GUI应用程序 / 代码
我在上面的5个步骤中解释了主要算法。在代码部分,我用注释解释了所有行。程序相当简单。用户使用颜色条选择一种颜色,然后程序获取那种颜色,处理它,并提取那种颜色的对象:
import cv2
import numpy as np
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
class ColorPickerApp:
def __init__(self, master):
self.master = master
self.master.title("Color Picker")
self.master.geometry("800x600") # Adjust the size of the window
# Create a frame to hold the color bar and color image
self.color_bar_frame = tk.Frame(master)
self.color_bar_frame.pack(side="top", fill="x", padx=5, pady=5)
self.hue_label = ttk.Label(self.color_bar_frame, text="Select Hue Value (10-179):")
self.hue_label.pack(side="left", padx=5, pady=5)
self.hue_scale = ttk.Scale(self.color_bar_frame, from_=10, to=179, orient="horizontal", command=self.update_color)
self.hue_scale.pack(side="left", padx=5, pady=5)
# Create a canvas for the color image
self.canvas_color = tk.Canvas(master, width=100, height=320)
self.canvas_color.pack(side="left", padx=5, pady=75)
# Create a canvas for the image
self.canvas_image = tk.Canvas(master, width=800, height=400)
self.canvas_image.pack(side="top", padx=5, pady=50)
self.detect_button = ttk.Button(master, text="Detect Objects", command=self.detect_objects)
self.detect_button.pack(side="top", padx=5, pady=5)
self.image = None
self.image_rgb = None
self.image_hsv = None
# Video capture
self.cap = cv2.VideoCapture("fish.mp4") # Change to 0 for webcam, or provide path for video file
# Load the initial frame
self.load_frame()
def load_frame(self):
ret, frame = self.cap.read()
if ret:
self.image = frame
self.image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.image_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Display the frame with detected regions
self.display_frame(self.image_rgb)
self.master.after(100, self.load_frame) # Continue to load frames
def update_color(self, value):
hue_value = int(float(value))
color_image = np.zeros((400, 100, 3), dtype=np.uint8)
color_image[:, :] = (hue_value, 255, 255)
color_image_rgb = cv2.cvtColor(color_image, cv2.COLOR_HSV2RGB)
color_image_rgb = Image.fromarray(color_image_rgb)
# Display the color image
color_image_tk = ImageTk.PhotoImage(image=color_image_rgb)
self.canvas_color.create_image(0, 0, anchor="nw", image=color_image_tk)
self.canvas_color.image = color_image_tk
def display_frame(self, frame):
img = Image.fromarray(frame)
# Get the original frame dimensions
frame_width, frame_height = img.size
# Define maximum width and height
max_width = 600
max_height = 300
# Calculate target width and height
target_width = min(frame_width, max_width)
target_height = min(frame_height, max_height)
# Calculate aspect ratio
aspect_ratio = frame_width / frame_height
# Adjust dimensions if necessary to fit within limits
if aspect_ratio > max_width / max_height:
target_width = max_width
target_height = int(target_width / aspect_ratio)
else:
target_height = max_height
target_width = int(target_height * aspect_ratio)
# Resize the frame while maintaining the aspect ratio
img = img.resize((target_width, target_height), Image.LANCZOS)
# Convert the resized frame to PhotoImage
img = ImageTk.PhotoImage(image=img)
# Clear previous frame and display the resized frame
self.canvas_image.delete("all")
self.canvas_image.create_image(0, 0, anchor="nw", image=img)
self.canvas_image.image = img
def detect_objects(self):
if self.image is None:
return
print("detecting objects")
# Define the hue range based on the current value of the hue scale
hue_value = int(self.hue_scale.get())
lower_limit = np.array([hue_value - 8, 100, 100])
upper_limit = np.array([hue_value + 8, 255, 255])
# Create a mask to detect objects within the specified hue range
mask = cv2.inRange(self.image_hsv, lower_limit, upper_limit)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Draw rectangles around the detected objects
for contour in contours:
print("contour found")
#if cv2.contourArea(contour) > 50:
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(self.image_rgb, (x, y), (x + w, y + h), (255, 255, 0), 5)
# Display the updated frame with detected objects
self.display_frame(self.image_rgb)
# Call detect_objects again after a delay
self.master.after(50, self.detect_objects)
def main():
root = tk.Tk()
app = ColorPickerApp(root)
root.mainloop()
if __name__ == "__main__":
main()