成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

交互式圖形用戶界面(GUI)應用程序 | 基于顏色的對象檢測和追蹤

人工智能 深度學習
在本文中,我將展示如何創建一個GUI,用于使用它們的顏色來檢測和追蹤對象。

大多數時候,用于對象檢測和追蹤的都是深度學習模型。的確,深度學習非常強大,但也存在其他的對象檢測和追蹤方法。在本文中,我將展示如何創建一個GUI,用于使用它們的顏色來檢測和追蹤對象。

檢測魚類

顏色可以用不同的格式表示。有多種方式來表示顏色:

  • 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()

責任編輯:趙寧寧 來源: 小白玩轉Python
相關推薦

2023-09-28 08:29:15

開源工具集語音識別

2023-04-10 15:08:52

Plotly Das開發Web 應用程序

2019-09-06 14:51:40

Python數據庫腳本語言

2011-09-01 15:54:10

app應用

2024-07-25 08:58:16

GradioPython數據應用

2025-05-06 00:00:00

CPU調度算法

2009-06-26 16:05:04

嵌入式Linux

2023-07-28 14:13:15

Streamlit開源Python庫

2021-10-18 13:31:28

Web應用交互式

2011-04-19 09:19:55

應用程序項目管理

2011-06-14 14:57:06

QT Python GUI

2009-06-10 14:59:04

Netbeans 6.應用程序

2015-07-14 09:50:28

PHPHTML5

2021-10-27 16:03:43

Python編程語言代碼

2024-08-02 10:30:39

StreamlitPython庫數據驅動

2011-06-21 11:10:28

Qt Embedded

2021-04-30 16:54:27

分散式應用程序

2010-02-26 14:40:15

Python應用程序

2023-12-10 14:43:30

PythonGUIeel

2019-07-23 23:11:21

JavaScript編程語言技術
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 精品亚洲一区二区三区 | 波多野结衣亚洲 | 日韩一区二区在线看 | a级大片免费观看 | 成人做爰www免费看视频网站 | 一级毛片视频在线 | 日韩视频a | 99re6在线视频精品免费 | 中文字幕视频一区二区 | 精品二区 | 日韩视频三区 | 超碰97人人人人人蜜桃 | 亚洲人成人一区二区在线观看 | 国内精品久久精品 | 国产一区二区 | 欧美1—12sexvideos | 久久i| 男女啪啪高潮无遮挡免费动态 | 国产色片| 免费国产一区二区 | 日韩一三区| www.欧美视频| 希岛爱理在线 | 成人深夜福利在线观看 | 欧美不卡| 欧美日韩精品综合 | 国产一区电影 | 365夜爽爽欧美性午夜免费视频 | 国产成人麻豆免费观看 | 中文日韩在线 | 五月激情婷婷六月 | 亚洲一区二区在线免费观看 | 亚洲色图综合 | 一a一片一级一片啪啪 | 中文字幕在线视频免费视频 | 91五月婷蜜桃综合 | 久久精品国产清自在天天线 | 中文字幕视频在线免费 | 欧美中文字幕在线 | 999久久| 国产伦一区二区三区视频 |