I want to find the red area for each divided image by dividing the image.

Asked 2 years ago, Updated 2 years ago, 95 views

I'd like to divide the images into nine parts and find the area in red for each image.
I tried my best, but if I move the code below, the same value will appear for 9 pieces.
I'd like to find the one with the larger value among the 9 pieces...
I'm not confident about the red HSV range either

Thank you for your cooperation.

import numpy as np
import cv2
import glob
importos

os.chdir("./test")

image=[ ]
image=glob.glob('*.png')
print(image)

def split_imgs(img, hsplits=3, vsplits=3):
    h,w = img.shape [:2]
    clop_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)]
    )

for i in image:
    
    img = cv2.imread(i)
        
    hsplits = 3# number of horizontal splits
    vsplits = 3# Number of vertical splits
    
    sub_imgs=split_imgs(img,hsplits,vsplits)
    print(sub_imgs.shape)#(9,140,186,3)
    
    for chin sub_imgs:
        # Convert to HSV color space.
        hsv = cv2.cvtColor (img, cv2.COLOR_BGR2HSV)
        # Red HSV Range
        hsv_min = np.array ([0,0,100])
        hsv_max = np.array ([255, 255, 255])

        mask = cv2.inRange(hsv, hsv_min, hsv_max)
        m=cv2.countNonZero(mask)
        h,w = mask.shape
        per = round (100*float(m)/(w*h), 1)
        print("Moment [px]:",m)
        print("Percent [%]:", per)

python image

2022-09-30 21:43

1 Answers

The same value appears because hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV).
This should be hsv=cv2.cvtColor(ch,cv2.COLOR_BGR2HSV).

I edited this post based on @metropolis's Comment and posted it as Community Wiki.I edited this post based on @metropolis's Comment and posted it as Community Wiki.


2022-09-30 21:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.