-3

For example,

For coordinate images (X, Y) namely (576, 0) until (726, 1371) I want to know which coordinates have pixel intensities in the range Red [165 to 225] Green [176 to 200] and Blue [186 to 198].

The output code is coordinate.

HansHirse
  • 18,010
  • 10
  • 38
  • 67
Mirasa
  • 23
  • 2
  • 2
    Can you please share what you tried yet ? – Anshuman Tiwari Feb 21 '20 at 05:07
  • ```inRange``` function or you can check each pixel's channel values as mentioned [here](https://stackoverflow.com/questions/28981417/how-do-i-access-the-pixels-of-an-image-using-opencv-python) – Yunus Temurlenk Feb 21 '20 at 05:33
  • there are two techniques: 1. subimaging 2. inRange function. You can use them in any order for your task. Afterwards you have to check each pixel of the subimage inRange result whether it is 0 or 255, maybe numpy has a function to do that for you, to extract a list – Micka Feb 21 '20 at 05:57

1 Answers1

2

Here is one way to do that with Python/OpenCV/Numpy.

  • Create a mask for the region
  • Create a mask from the colors
  • Combine masks
  • Get coordinates where combined mask is not black

Input:

enter image description here

import cv2
import numpy as np

# load image
img = cv2.imread("monet2.jpg")

# create region mask
mask1 = np.zeros_like(img)[:,:,0]
mask1[0:0+75, 90:90+75] = 255

# create color mask
lower =(0,100,150) # lower bound for each channel
upper = (40,160,2100) # upper bound for each channel
mask2 = cv2.inRange(img, lower, upper)

# combine masks
mask3 = cv2.bitwise_and(mask1, mask2)

# get coordinates
coords = np.argwhere(mask3)
for p in coords:
    px = (p[0],p[1])
    print (px)


# apply mask to image (to see where data is obtained)
mask3 = cv2.merge([mask3,mask3,mask3])
img_masked = cv2.bitwise_and(img, mask3)


# display images
cv2.imshow("mask1", mask1)
cv2.imshow("mask2", mask2)
cv2.imshow("mask3", mask3)
cv2.imshow("img_masked", img_masked)
cv2.waitKey(0)

# write results to disk
cv2.imwrite("monet2_mask1.jpg", mask1)
cv2.imwrite("monet2_mask2.jpg", mask2)
cv2.imwrite("monet2_mask3.jpg", mask3)
cv2.imwrite("monet2_masked.jpg", img_masked)


Region Mask:

enter image description here

Color Mask:

enter image description here

Combined Mask:

enter image description here

Masked Image:

enter image description here

Coordinates List:

(6, 128)
(7, 122)
(7, 125)
...
(63, 125)
(63, 126)
(63, 134)
(63, 135)
fmw42
  • 46,825
  • 10
  • 62
  • 80