Hướng dẫn find color in image python - tìm màu trong hình ảnh python

Tất cả các câu trả lời thảo luận về các phương pháp để tìm một màu duy nhất trong một hình ảnh nhưng biết cách tìm nhiều màu trong hình ảnh luôn có lợi. Đặc biệt là khi bạn đối phó với hình ảnh của nhiệm vụ phân khúc.

Hãy chụp ảnh cho lời giải thích của chúng tôi

Hướng dẫn find color in image python - tìm màu trong hình ảnh python

Rõ ràng, mỗi lớp đối tượng trong bức tranh này có một màu khác nhau.

Hãy viết một chức năng để tải xuống một hình ảnh từ URL và chuyển đổi nó thành một mảng numpy. Nó trở nên rất dễ dàng để đối phó với hình ảnh theo cách này.

import numpy as np
import cv2
import urllib
from urllib.request import urlopen
import webcolors
import time

def getImageArray(mask):
    req = requestObject(mask) 
    arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
    im = cv2.imdecode(arr, -1) 
    im = im[:, :, :3] 
    return im

def requestObject(mask):
    temp_req = {'status': 403}
    retry_counter = 1
    while((temp_req['status'] != 200) and (retry_counter <= 10)):
        try:
            req = urlopen(mask)
            temp_req = {"status": 200}
        except:
            print("Retrying for: ", retry_counter)
            temp_req = {"status": 403}
            time.sleep(4)
        retry_counter = retry_counter + 1

    return req

Bây giờ, hãy lấy hình ảnh:

url = 'https://i.stack.imgur.com/Bi16j.jpg'
image = getImageArray(url)

Hãy viết một chức năng để tìm tất cả các màu:

def bgr_to_hex(bgr):
   rgb =list(bgr)
   rgb.reverse()
   return webcolors.rgb_to_hex(tuple(rgb))

def FindColors(image):
    color_hex = []
    for i in image:
        for  j in i:
            j = list(j)
            color_hex.append(bgr_to_hex(tuple(j)))
    return set(color_hex)

color_list = FindColors(image)

Hãy thử chạy tập lệnh trên trong thiết bị đầu cuối của bạn và bạn sẽ nhận được danh sách các mã thập lục phân cho tất cả các màu trong biến color_list.

Hãy cho tôi biết nếu mã hoạt động/không dành cho bạn :)

Một thư viện nguồn mở trong Python, OpenCV về cơ bản được sử dụng để xử lý hình ảnh và video. Không chỉ được hỗ trợ bởi bất kỳ hệ thống nào, chẳng hạn như Windows, Linux, Mac, v.v. mà còn có thể được chạy trong bất kỳ ngôn ngữ lập trình nào như Python, C ++, Java, v.v. OpenCV cũng cho phép bạn xác định màu trong hình ảnh. Bạn có biết làm thế nào để tìm thấy những màu này trong hình ảnh không?OpenCV is basically used for image and video processing. Not only supported by any system, such as Windows, Linux, Mac, etc. but also it can be run in any programming language like Python, C++, Java, etc. OpenCV also allows you to identify color in images. Don’t you know how to find these colors in images?

Nhận dạng màu trong hình ảnh

Một màn hình hoặc màn hình TV về cơ bản tạo ra ba loại màu, tức là màu đỏ, xanh lá cây và vàng. Nhưng sự kết hợp và cường độ của ba màu này tạo nên nhiều màu sắc khác nhau. Do đó, mỗi màu có mã màu HSV duy nhất. Để tìm màu được chỉ định trong hình ảnh đã cho, chúng ta cần sử dụng giới hạn dưới và trên của màu đó.HSV color code. For finding the specified color in the given image, we need to use the lower and upper bound of that color.

Ví dụ: Để tìm màu xanh lá cây trong hình ảnh, chúng ta cần chỉ định mã màu HSV dưới và trên cho màu xanh lá cây như sau. For finding the green color in the image, we need to specify the lower and upper HSV color code for green color as follows.

lower=np.array([50, 100,100])
upper=np.array([70, 255, 255])

Làm thế nào để xác định màu sắc trong opencv?

Hãy cùng cố gắng thực hiện phương pháp này thực tế. Ví dụ, hãy xem xét nguồn trang dưới đây. Trong mã nguồn này, chúng tôi đang tìm màu xanh lá cây trong hình ảnh hình dạng.jpg. Hình ảnh được sử dụng trong ví dụ được đưa ra dưới đây & nbsp;shapes.jpg image. The image used in the example given below 

Hướng dẫn find color in image python - tìm màu trong hình ảnh python

Bạn có thể tải xuống và lưu nó trong cùng một thư mục nơi bạn đã lưu tệp Python mà bạn hiện đang viết. & NBSP;

Implementation:

Python

import cv2

import

url = 'https://i.stack.imgur.com/Bi16j.jpg'
image = getImageArray(url)
0

color_list0

url = 'https://i.stack.imgur.com/Bi16j.jpg'
image = getImageArray(url)
2 color_list2
lower = [50, 100, 100]
upper = [70, 255, 255]
4
def bgr_to_hex(bgr):
   rgb =list(bgr)
   rgb.reverse()
   return webcolors.rgb_to_hex(tuple(rgb))

def FindColors(image):
    color_hex = []
    for i in image:
        for  j in i:
            j = list(j)
            color_hex.append(bgr_to_hex(tuple(j)))
    return set(color_hex)

color_list = FindColors(image)
0
lower = [h-10, 100, 100]
upper = [h+10, 255, 255]
0
def bgr_to_hex(bgr):
   rgb =list(bgr)
   rgb.reverse()
   return webcolors.rgb_to_hex(tuple(rgb))

def FindColors(image):
    color_hex = []
    for i in image:
        for  j in i:
            j = list(j)
            color_hex.append(bgr_to_hex(tuple(j)))
    return set(color_hex)

color_list = FindColors(image)
0
lower = [50, 100, 100]
upper = [70, 255, 255]
4color_list8

color_list9

url = 'https://i.stack.imgur.com/Bi16j.jpg'
image = getImageArray(url)
2 import1

def bgr_to_hex(bgr):
   rgb =list(bgr)
   rgb.reverse()
   return webcolors.rgb_to_hex(tuple(rgb))

def FindColors(image):
    color_hex = []
    for i in image:
        for  j in i:
            j = list(j)
            color_hex.append(bgr_to_hex(tuple(j)))
    return set(color_hex)

color_list = FindColors(image)
3
url = 'https://i.stack.imgur.com/Bi16j.jpg'
image = getImageArray(url)
2
def bgr_to_hex(bgr):
   rgb =list(bgr)
   rgb.reverse()
   return webcolors.rgb_to_hex(tuple(rgb))

def FindColors(image):
    color_hex = []
    for i in image:
        for  j in i:
            j = list(j)
            color_hex.append(bgr_to_hex(tuple(j)))
    return set(color_hex)

color_list = FindColors(image)
5

def bgr_to_hex(bgr):
   rgb =list(bgr)
   rgb.reverse()
   return webcolors.rgb_to_hex(tuple(rgb))

def FindColors(image):
    color_hex = []
    for i in image:
        for  j in i:
            j = list(j)
            color_hex.append(bgr_to_hex(tuple(j)))
    return set(color_hex)

color_list = FindColors(image)
6
url = 'https://i.stack.imgur.com/Bi16j.jpg'
image = getImageArray(url)
2
def bgr_to_hex(bgr):
   rgb =list(bgr)
   rgb.reverse()
   return webcolors.rgb_to_hex(tuple(rgb))

def FindColors(image):
    color_hex = []
    for i in image:
        for  j in i:
            j = list(j)
            color_hex.append(bgr_to_hex(tuple(j)))
    return set(color_hex)

color_list = FindColors(image)
8
def bgr_to_hex(bgr):
   rgb =list(bgr)
   rgb.reverse()
   return webcolors.rgb_to_hex(tuple(rgb))

def FindColors(image):
    color_hex = []
    for i in image:
        for  j in i:
            j = list(j)
            color_hex.append(bgr_to_hex(tuple(j)))
    return set(color_hex)

color_list = FindColors(image)
9
def bgr_to_hex(bgr):
   rgb =list(bgr)
   rgb.reverse()
   return webcolors.rgb_to_hex(tuple(rgb))

def FindColors(image):
    color_hex = []
    for i in image:
        for  j in i:
            j = list(j)
            color_hex.append(bgr_to_hex(tuple(j)))
    return set(color_hex)

color_list = FindColors(image)
0
lower=np.array([50, 100,100])
upper=np.array([70, 255, 255])
1
def bgr_to_hex(bgr):
   rgb =list(bgr)
   rgb.reverse()
   return webcolors.rgb_to_hex(tuple(rgb))

def FindColors(image):
    color_hex = []
    for i in image:
        for  j in i:
            j = list(j)
            color_hex.append(bgr_to_hex(tuple(j)))
    return set(color_hex)

color_list = FindColors(image)
0
lower=np.array([50, 100,100])
upper=np.array([70, 255, 255])
1
lower=np.array([50, 100,100])
upper=np.array([70, 255, 255])
4

lower=np.array([50, 100,100])
upper=np.array([70, 255, 255])
5
url = 'https://i.stack.imgur.com/Bi16j.jpg'
image = getImageArray(url)
2
def bgr_to_hex(bgr):
   rgb =list(bgr)
   rgb.reverse()
   return webcolors.rgb_to_hex(tuple(rgb))

def FindColors(image):
    color_hex = []
    for i in image:
        for  j in i:
            j = list(j)
            color_hex.append(bgr_to_hex(tuple(j)))
    return set(color_hex)

color_list = FindColors(image)
8
lower=np.array([50, 100,100])
upper=np.array([70, 255, 255])
8
def bgr_to_hex(bgr):
   rgb =list(bgr)
   rgb.reverse()
   return webcolors.rgb_to_hex(tuple(rgb))

def FindColors(image):
    color_hex = []
    for i in image:
        for  j in i:
            j = list(j)
            color_hex.append(bgr_to_hex(tuple(j)))
    return set(color_hex)

color_list = FindColors(image)
0
lower = [h-10, 100, 100]
upper = [h+10, 255, 255]
0
def bgr_to_hex(bgr):
   rgb =list(bgr)
   rgb.reverse()
   return webcolors.rgb_to_hex(tuple(rgb))

def FindColors(image):
    color_hex = []
    for i in image:
        for  j in i:
            j = list(j)
            color_hex.append(bgr_to_hex(tuple(j)))
    return set(color_hex)

color_list = FindColors(image)
0
lower = [h-10, 100, 100]
upper = [h+10, 255, 255]
0
lower=np.array([50, 100,100])
upper=np.array([70, 255, 255])
4

lower = [h-10, 100, 100]
upper = [h+10, 255, 255]
4
url = 'https://i.stack.imgur.com/Bi16j.jpg'
image = getImageArray(url)
2
lower = [h-10, 100, 100]
upper = [h+10, 255, 255]
6

lower = [h-10, 100, 100]
upper = [h+10, 255, 255]
7
lower = [h-10, 100, 100]
upper = [h+10, 255, 255]
8
lower = [h-10, 100, 100]
upper = [h+10, 255, 255]
9

lower = [h-10, 100, 100]
upper = [h+10, 255, 255]
7
lower = [50, 100, 100]
upper = [70, 255, 255]
1
lower = [50, 100, 100]
upper = [70, 255, 255]
2

lower = [50, 100, 100]
upper = [70, 255, 255]
3
lower = [50, 100, 100]
upper = [70, 255, 255]
4
url = 'https://i.stack.imgur.com/Bi16j.jpg'
image = getImageArray(url)
5

Output:

Hướng dẫn find color in image python - tìm màu trong hình ảnh python

Tìm mã HSV từ mã RGB

Bạn có biết làm thế nào để tìm mã màu HSV độc đáo này không? Chỉ cần viết chương trình dưới đây để tìm ra mã màu HSV từ mã màu RGB. Bạn có thể chọn mã màu RGB của màu bạn muốn từ đây. Sau khi tìm mã RGB cho màu bạn muốn tìm, hãy viết chương trình này vào mã HSV cho màu đó. & NBSP;here. After finding the RGB code for the color you want to find, write this program to HSV code for that color. 

Ví dụ: Trong mã nguồn dưới đây, chúng tôi đã tìm thấy giá trị HSV cho màu xanh lá cây. Giá trị RGB cho màu xanh lá cây là [0, 255, 0]. In the below source code, we have found the HSV value for the green color. The RGB value for the green color is [0, 255, 0].

Python

import cv2

import

url = 'https://i.stack.imgur.com/Bi16j.jpg'
image = getImageArray(url)
0

color_list0

url = 'https://i.stack.imgur.com/Bi16j.jpg'
image = getImageArray(url)
2 color_list2
lower = [50, 100, 100]
upper = [70, 255, 255]
4
def bgr_to_hex(bgr):
   rgb =list(bgr)
   rgb.reverse()
   return webcolors.rgb_to_hex(tuple(rgb))

def FindColors(image):
    color_hex = []
    for i in image:
        for  j in i:
            j = list(j)
            color_hex.append(bgr_to_hex(tuple(j)))
    return set(color_hex)

color_list = FindColors(image)
0
lower = [h-10, 100, 100]
upper = [h+10, 255, 255]
0
def bgr_to_hex(bgr):
   rgb =list(bgr)
   rgb.reverse()
   return webcolors.rgb_to_hex(tuple(rgb))

def FindColors(image):
    color_hex = []
    for i in image:
        for  j in i:
            j = list(j)
            color_hex.append(bgr_to_hex(tuple(j)))
    return set(color_hex)

color_list = FindColors(image)
0
lower = [50, 100, 100]
upper = [70, 255, 255]
4color_list8

color_list9

url = 'https://i.stack.imgur.com/Bi16j.jpg'
image = getImageArray(url)
2 import1

import2import3

lower = [50, 100, 100]
upper = [70, 255, 255]
3
lower = [50, 100, 100]
upper = [70, 255, 255]
4
url = 'https://i.stack.imgur.com/Bi16j.jpg'
image = getImageArray(url)
5

Output:

Hướng dẫn find color in image python - tìm màu trong hình ảnh python

Một lần, bạn đã tìm thấy mã HSV duy nhất cho một màu cụ thể, hãy liên kết với HSV thấp hơn và giới hạn HSV trên của màu đó bằng cách làm theo các bước dưới đây.

lower = [h-10, 100, 100]
upper = [h+10, 255, 255]

Ví dụ: Đối với màu xanh lá cây, mã màu HSV là [60, 255, 255].Do đó, giới hạn HSV dưới và trên của màu đó sẽ như sau. For the green color, HSV color code is [60, 255, 255]. Hence, the lower and upper HSV bound of that color will be as follows.

lower = [50, 100, 100]
upper = [70, 255, 255]

Màu sắc () làm gì trong Python?

Hàm color () vẽ một màu trên hình ảnh bằng màu hiện tại, bắt đầu ở vị trí & phương thức được chỉ định.draws a color on the image using current fill color, starting at specified position & method.

OpenCV có thể phát hiện màu sắc không?

OpenCV có một số chức năng tích hợp để thực hiện các hoạt động phát hiện và phân đoạn màu.Vậy phát hiện màu và kỹ thuật phân đoạn trong xử lý hình ảnh là gì?Phát hiện màu là một kỹ thuật phát hiện bất kỳ màu nào trong một phạm vi HSV (giá trị bão hòa HUE) nhất định.. So what are Color Detection and Segmentation Techniques in Image Processing? Color detection is a technique of detecting any color in a given range of HSV (hue saturation value) color space.

Làm thế nào bạn có thể xác định màu sắc?

Cảm biến màu phát hiện màu sắc bằng cách phát ra ánh sáng (RGB, đỏ, xanh lá cây, xanh dương) lên một bề mặt.Các giá trị màu được tính từ các chùm phản xạ và so sánh với các giá trị tham chiếu được lưu trữ trước đó.by emitting light (RGB, red, green, blue) to a surface. The colour values ​​are calculated from the reflecting beams and compared with previously stored reference values.