Home>
I want to divide an image with python, find the HSV value for each divided image, and visualize it.
I saw the code of the answer of another answerer (tiitoi) (the code below), but I still get an error and I would like to refer to it, but it is not working.
I'm not sure how to define channels.
There are some points that cannot be reached with the first question, but please teach someone.
import cv2
import matplotlib.pyplot as plt
import numpy as np
def split_imgs (img, hsplits = 3, vsplits = 3):
h, w = img.shape [: 2]
crop_img = img [: h // vsplits * vsplits,: w // hsplits * hsplits]
return np.array (
[x for h_img in np.vsplit (crop_img, vsplits) for x in np.hsplit (h_img, hsplits)]
)
def hsv_hist (img):
# Convert to HSV color space.
hsv = cv2.cvtColor (img, cv2.COLOR_BGR2HSV)
#Calculate the histogram for each channel.
hists = []
for ch in range (3):
hist = cv2.calcHist ([hsv], [ch], None, [256], [0, 256])
hists.append (hist)
return hists
#Load the image.
hsplits = 3 # number of horizontal divisions
vsplits = 3 # number of vertical divisions
img = cv2.imread ("17.4.png")
#Split the image.
sub_imgs = split_imgs (img, hsplits, vsplits)
print (sub_imgs.shape) # (9, 140, 186, 3)
# Get a histogram of each image.
hists = np.array ([hsv_hist (x) for x in sub_imgs])
print (hists.shape) # (9, 3, 256, 1)
# Draw a histogram.
ch_names = {0: "H", 1: "S", 2: "V"}
fig, axes = plt.subplots (hsplits, vsplits, figsize = (10, 10))
for ax, hsv_hist in zip (axes.ravel (), hists):
# Draw each histogram.
for hist, [ch] in zip (hsv_hist, channels):
ax.plot (hist, label = ch_names [ch])
ax.set_xlim ([0, 256])
ax.set_xlabel ("Pixel Value")
ax.legend ()
plt.show ()
As you can see,'channels' is not defined and it doesn't work.
-------------------------------------------------- -------------------------
NameError Traceback (most recent call last)
<ipython-input-38-4513b313dd4e>in<module>
45 # Draw each histogram.
46
--->47 for hist, [ch] in zip (hsv_hist, channels):
48 ax.plot (hist, label = ch_names [ch])
49 ax.set_xlim ([0, 256])
NameError: name'channels' is not defined
Supplementary information (FW/tool version, etc.)
python 3.7.6
-
Answer # 1
Related articles
- python - i want to divide the read image into pixels and label them
- how to save the image ocr result file in python
- put the maximum value of the list of variables in the objective function with python pulp
- i want to set the maximum value of the slider in python to the number entered in the text box
- python - how to output the key and value in the dictionary in any form
- python 3x - how to get the value of scrolledtext
- python - image recognition using cnn keras multiple inputs
- python - i want to read an image and display it
- python - i want to create a contour image
- i want to get the value from a constant in python and display it
- python - i want to display an image with pysimplegui, but an error occurs
- python - i want to convert a black and white image to an arbitrary color image
- python - about image deletion in django
- python 3x - i want to save the value of a variable in memory
- python - judgment if the value is nan or not a valid url
- python - error in image binarization using cv2adaptivethreshold function
- about image output of python
- python 3x - the output result of the numerical value obtained by web scraping becomes 0
- python - image cannot be read by opencv
- python - when i tried to inflate the image, i couldn't read the image
Related questions
- python - image display from the variable to which the file name is assigned
- python - 4 resolved title
- how to save the image ocr result file in python
- python - i want to find the number of data per row in pandas dataframe
- command does not start in python discordbot
- python - pandas dataframe comparison (dfequals) doesn't work is it because the individual element read from the csv file has a d
- python - [django] isn't the "except pagenoeaninteger" block running in viewspy?
- error in python, subprocess
- [python] functions in the class that inherits formatter do not work
- error in python/discord bot
Is this the source of the citation?
https://stackoverflow.com/questions/192394
I tried to fix it as follows.