Regarding the title, I was at a standstill because I didn't know the cause.
In Python, I have written a program to compare the following two HSV values.
Both were originally the same image, so I assumed the HSV value to be the same, but as a result, there was a difference.
I would like to know if anyone knows how to read and convert, or if there is a difference in specifications.
Python 3.8.8
cv2.version'4.5.3'
np.version'1.20.1'
pyautogui.version'0.9.53'
Difference between hsvList1 and hsvList2
import pyautogui
import cv2
import numpy as np
# Set Acquisition Coordinates
x = 1000
y = 1000
width = 1000
height = 1000
# Get a screenshot
sc=pyautogui.screenshot(region=(x,y,width,high))
# Convert to np.array
sc_im=np.array(sc,dtype='uint8')
# Convert from BGR to HSV
sc_im = cv2.cvtColor(sc_im, cv2.COLOR_BGR2HSV)
# Obtain HSV maximum and minimum values
h_min_1 = sc_im.T[0].flatten().min()
h_max_1 = sc_im.T[0].flatten().max()
s_min_1=sc_im.T[1].flatten().min()
s_max_1 = sc_im.T[1].flatten().max()
v_min_1=sc_im.T[2].flatten().min()
v_max_1=sc_im.T[2].flatten().max()
hsvList1 = [h_min_1, h_max_1, s_min_1, s_max_1, v_min_1, v_max_1]
print(str(hsvList1))
# convert to RGB and save
sc=sc.convert('RGB')
sc.save("test.jpg")
# Load saved files
n=np.fromfile("test.jpg", np.uint8)
file_im=cv2.imdecode(n,flags=cv2.IMREAD_COLOR)
# Convert to HSV
file_im = cv2.cvtColor(file_im, cv2.COLOR_BGR2HSV)
# Obtain HSV maximum and minimum values
h_min_2=file_im.T[0].flatten().min()
h_max_2 = file_im.T[0].flatten().max()
s_min_2=file_im.T[1].flatten().min()
s_max_2=file_im.T[1].flatten().max()
v_min_2=file_im.T[2].flatten().min()
v_max_2=file_im.T[2].flatten().max()
hsvList2 = [h_min_2, h_max_2, s_min_2, s_max_2, v_min_2, v_max_2]
print(str(hsvList2))
[0,176,0,255,2,255]
[0, 179, 0, 255, 0, 255]
I have received your reply and solved it.The causes and actions are as follows:
◆Cause
The image saved in JPEG format (irreversible compression) was read, so it was different from when taking screenshots (as per specifications).
◆ Countermeasures
Save the image in PNG format and read it. (Verified there is no difference)
◆ Supplemental
According to the source code, after loading the image, it says cv2.COLOR_BGR2HSV, but cv2.COLOR_RGB2HSV is correct.
© 2024 OneMinuteCode. All rights reserved.