There is a difference between the point of acquisition with pyautogui and the HSV value when reloading after saving the same image.

Asked 1 years ago, Updated 1 years ago, 97 views

Regarding the title, I was at a standstill because I didn't know the cause.

Questions

In Python, I have written a program to compare the following two HSV values.

  • Convert images from pyautogui.screenshot to np.array→Convert cv2.cvtColor to HSV to get values
  • Read np.fromfile→cv2.imdecode→cv2.cv2.cvtColor to get the value for the file where the above is saved in RGB

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.

environment

Python 3.8.8
cv2.version'4.5.3'
np.version'1.20.1'
pyautogui.version'0.9.53'

Source

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

print Sample Output

 [0,176,0,255,2,255]
[0, 179, 0, 255, 0, 255]

python opencv numpy image pyautogui

2022-09-30 15:46

1 Answers

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.
 


2022-09-30 15:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.