# Unsharp masking

**URL:** <https://forum.opencv.org/t/unsharp-masking/9004>\
**Category:** C++\
**Tags:** imgproc\
**Created:** [June 1, 2022, 10:28pm UTC](https://forum.opencv.org/t/unsharp-masking/9004 "2022-06-01T22:28:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanya](https://avatars.discourse-cdn.com/v4/letter/s/b19c9b/32.png) [@sanya](https://forum.opencv.org/u/sanya)\
**Post date:** [June 1, 2022, 10:28pm UTC](https://forum.opencv.org/t/unsharp-masking/9004/1 "2022-06-01T22:28:56Z")

</div>

I am new to opencv and I am trying to perform unsharp masking ,however there some errors that that the resulting pixels can be \<0 or \>255 so they need to be scaled down.

```auto
src.at<uchar>(y,x) + (k * (float)(src.at<uchar>(y, x) - res.at<uchar>(y, x)))

```

can be greater than 255 and can be smaller than 0.

---

<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:** [June 2, 2022, 6:26am UTC](https://forum.opencv.org/t/unsharp-masking/9004/2 "2022-06-02T06:26:36Z")

</div>

> [@sanya](#):
>
> I am trying to perfom unsharp masking

please explain some more. what exactly are you doing ? what is the purpose ?

can you try again with the formula ?  
also, it’s unclear: do you want a saturated (clipped) result, or not ?

---

<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:** [June 2, 2022, 8:40am UTC](https://forum.opencv.org/t/unsharp-masking/9004/3 "2022-06-02T08:40:25Z")

</div>

related: [Unsharp Masking opencv c++ - Stack Overflow](https://stackoverflow.com/questions/72468913/unsharp-masking-opencv-c)

please be more careful with copy-pasting. your paste here was broken.

---

<div class="post-metadata">

**Author:** ![sanya](https://avatars.discourse-cdn.com/v4/letter/s/b19c9b/32.png) [@sanya](https://forum.opencv.org/u/sanya)\
**Post date:** [June 2, 2022, 2:00pm UTC](https://forum.opencv.org/t/unsharp-masking/9004/4 "2022-06-02T14:00:17Z")

</div>

Yes,exactly…to have the values in some range.  
a) (k \* (float)(src.at(y, x) - res.at(y, x))) can be negative,  
b) src.at(y,x) + (k \* (float)(src.at(y, x) - res.at(y, x))) can be greater than 255 and can be smaller than 0.  
Performing a saturated add

I posted here earlier but I wouldnt post the whole code unfortunately,buts its there on stackoverflow
