# Treating Against the Light Photos to Detect Red Marker from the Sky

**URL:** <https://forum.opencv.org/t/treating-against-the-light-photos-to-detect-red-marker-from-the-sky/9397>\
**Category:** Python\
**Tags:** imgproc\
**Created:** [July 5, 2022, 2:50am UTC](https://forum.opencv.org/t/treating-against-the-light-photos-to-detect-red-marker-from-the-sky/9397 "2022-07-05T02:50:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![angela804](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/angela804/32/5700_2.png) [@angela804](https://forum.opencv.org/u/angela804)\
**Post date:** [July 5, 2022, 2:50am UTC](https://forum.opencv.org/t/treating-against-the-light-photos-to-detect-red-marker-from-the-sky/9397/1 "2022-07-05T02:50:04Z")

</div>

Hi! I’m currently working on a project right now that is able to detect red markers from the sky.  
My color masks are working great.

However, when I tried testing the algorithm by taking pictures of red markers from the sky, I ran across the problem of having against the Light Photos.  
My red markers go dark and the pixels don’t become red anymore. See the photo.

 ![marker2](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/0/0aaa190a0e3b1a21b5205b4d23d574b06b121436.png)

Only one pixel is detected that is red. Shown in the picture, only one pixel is color white.

So, I’m curious if there are opencv-python treatments to preprocess this image first, or capturing images issue, or it can’t be solved at all? Thanks!

Here’s my code:

```auto
path = r'C:\Users\ #location of the image
image = cv2.imread(path, 1)
img = image

img_hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# lower mask (0-10)
lower_red = np.array([0,100,100]) # 0,50,50 original
upper_red = np.array([10,255,255]) 
mask0 = cv2.inRange(img_hsv, lower_red, upper_red) 

# upper mask (170-180)
lower_red = np.array([160,100,100]) # 170,50,50 original
upper_red = np.array([180,255,255])
mask1 = cv2.inRange(img_hsv, lower_red, upper_red)

mask = mask0+mask1

output_img = img.copy()
output_img[np.where(mask==0)] = 0
output_img[np.where(mask!=0)] = 255

```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [July 5, 2022, 6:08am UTC](https://forum.opencv.org/t/treating-against-the-light-photos-to-detect-red-marker-from-the-sky/9397/2 "2022-07-05T06:08:35Z")

</div>

what are you doing , so far, to detect the markers ? please show code.  
(maybe it can be improved by using hsv colorspace ? who knows …)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [July 5, 2022, 7:26am UTC](https://forum.opencv.org/t/treating-against-the-light-photos-to-detect-red-marker-from-the-sky/9397/3 "2022-07-05T07:26:58Z")

</div>

you could control your camera’s exposure. the marker will look better when the sky is overexposed.

and yes, we need to see code. there’s no reason _that_ picture shouldn’t work. marker is red and saturated and not too dark to make those values noisy. I tried that just now and it works just fine.

---

<div class="post-metadata">

**Author:** ![angela804](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/angela804/32/5700_2.png) [@angela804](https://forum.opencv.org/u/angela804)\
**Post date:** [July 5, 2022, 7:41am UTC](https://forum.opencv.org/t/treating-against-the-light-photos-to-detect-red-marker-from-the-sky/9397/4 "2022-07-05T07:41:15Z")

</div>

Hi. I edited the post with the code. Thanks a lot 😃

---

<div class="post-metadata">

**Author:** ![angela804](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/angela804/32/5700_2.png) [@angela804](https://forum.opencv.org/u/angela804)\
**Post date:** [July 5, 2022, 7:41am UTC](https://forum.opencv.org/t/treating-against-the-light-photos-to-detect-red-marker-from-the-sky/9397/5 "2022-07-05T07:41:55Z")

</div>

Oh. Can you teach me how? I edited my post together with the code.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [July 5, 2022, 8:02am UTC](https://forum.opencv.org/t/treating-against-the-light-photos-to-detect-red-marker-from-the-sky/9397/6 "2022-07-05T08:02:04Z")

</div>

play with the lower bound’s _Value_ value. the marker is darker than 100.

generally, play with parameters.

also: you _ **should not use `np.where`** _ in your code at all.

- it’s not needed. simply use a mask as “the index”: `output_img[mask==0] = 0`
- np.where is doing one of two things depending on number of arguments. if you must, use `np.nonzero`

I’ve seen people abuse np.where a lot recently. did someone make a new youtube tutorial?

---

<div class="post-metadata">

**Author:** ![angela804](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/angela804/32/5700_2.png) [@angela804](https://forum.opencv.org/u/angela804)\
**Post date:** [July 5, 2022, 10:14am UTC](https://forum.opencv.org/t/treating-against-the-light-photos-to-detect-red-marker-from-the-sky/9397/7 "2022-07-05T10:14:15Z")

</div>

actually, _ **np.where** _ is the code I used to convert the image to binary, 255 for the detected red and 0 for the undetected. I’ve been doing OpenCV and I’m still not an expert.  
So based on what you recommend, np.nonzero could probably lessen my runtime error.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [July 5, 2022, 11:58am UTC](https://forum.opencv.org/t/treating-against-the-light-photos-to-detect-red-marker-from-the-sky/9397/8 "2022-07-05T11:58:30Z")

</div>

if you use `np.where(mask_array, consequence_values, alternative_values)` (three arguments), and you need the effect it produces, then it’s the best tool for the task.

if you’re just overwriting parts of an array using a mask, you can just do

```python
im[mask_array] = consequence_values[mask_array]
# and/or
im[~mask_array] = alternative_values[~mask_array]

```

in case the values arrays are arrays. if not, the indexing on the right hand side doesn’t happen at all.
