# Fail to assign value at (y,x)

**URL:** <https://forum.opencv.org/t/fail-to-assign-value-at-y-x/15554>\
**Category:** C++\
**Created:** [November 16, 2023, 6:07pm UTC](https://forum.opencv.org/t/fail-to-assign-value-at-y-x/15554 "2023-11-16T18:07:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![amiramyss](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/amiramyss/32/9101_2.png) [@amiramyss](https://forum.opencv.org/u/amiramyss)\
**Post date:** [November 16, 2023, 6:07pm UTC](https://forum.opencv.org/t/fail-to-assign-value-at-y-x/15554/1 "2023-11-16T18:07:12Z")

</div>

Hi, I’m trying to make a single pixel white like this:  
`vertical_filtered_mat.at<uint>(y, x) = 255;`  
And it throws these COMPILATION errors:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/e/e8cf00a7dbe6268ce6111ee6f381d83ffe8a0376.png)  
How can I fix this?  
src code:

> inline Point catch\_feature( Mat img,  
> uint8\_t threshold = 50,  
> uint8\_t shrink\_factor = 5,  
> uint8\_t ker\_dim = 3) {

> // vars  
> int height = img.cols;  
> int width = img.cols;

```
int filtered_met_size = height - ker_dim + 1;

Mat vertical_filtered_mat = Mat::zeros(Size(filtered_met_size / shrink_factor, filtered_met_size / shrink_factor), CV_8U);
Mat horizontal_filtered_mat = Mat::zeros(Size(filtered_met_size / shrink_factor, filtered_met_size / shrink_factor), CV_8U);

// logic
// vertical seperator
for (size_t y = 1; y < height - 1; y += shrink_factor) {
	for (size_t x = 1; x < width - 2; x += shrink_factor) {
		// check if 2 places to the right is brighter
		if (img.at<uchar>(y, x + 2) - img.at<uchar>(y, x) > threshold) {
			for (int dy = -((ker_dim - 1) / 2); dy <= (ker_dim - 1) / 2; dy++) {
				for (int dx = -((ker_dim - 1) / 2); dx <= (ker_dim - 1) / 2; dx++) {
					if (img.at<uchar>(y + dy, x + dx + 2) - img.at<uchar>(y + dy, x + dx) > threshold) {
						vertical_filtered_mat.at<uint>(y, x) = 255; // ERROR HERE
					}
				}
			}
		}
	}
}

return Point(-1, -1);

```

}

Thanks 🙂

---

<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:** [November 16, 2023, 6:14pm UTC](https://forum.opencv.org/t/fail-to-assign-value-at-y-x/15554/2 "2023-11-16T18:14:01Z")

</div>

insufficient. [MRE](https://stackoverflow.com/help/minimal-reproducible-example) please.

---

<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:** [November 16, 2023, 7:23pm UTC](https://forum.opencv.org/t/fail-to-assign-value-at-y-x/15554/3 "2023-11-16T19:23:16Z")

</div>

> [@amiramyss](#):
>
> `at<uint>(y, x)`

there’ s no such thing as an uint _pixel_ format in opencv  
(you probably _mean_ `uchar`?)  
((your Mat was allocated as CV\_8U, so you _must_ use uchar for get / set ops))  
(((that said, please **avoid** code like above. it’s slow & error prone)))

---

<div class="post-metadata">

**Author:** ![amiramyss](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/amiramyss/32/9101_2.png) [@amiramyss](https://forum.opencv.org/u/amiramyss)\
**Post date:** [November 16, 2023, 7:42pm UTC](https://forum.opencv.org/t/fail-to-assign-value-at-y-x/15554/4 "2023-11-16T19:42:38Z")

</div>

oh yea, that was it… awkward 0\_0 … Thanks! 🙂

---

<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:** [November 16, 2023, 7:46pm UTC](https://forum.opencv.org/t/fail-to-assign-value-at-y-x/15554/5 "2023-11-16T19:46:56Z")

</div>

mind challenge: try to _vectorize this_

e.g : don’t try to compare a single pixel to it’s neighbour , 2 pix right, but compare a whole Mat to another (slice), _shifted_ 2 pix to the right 😉

also, for convolution, there’s filter2D()

NEVER WRITE FOR LOOPS HERE !

---

<div class="post-metadata">

**Author:** ![amiramyss](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/amiramyss/32/9101_2.png) [@amiramyss](https://forum.opencv.org/u/amiramyss)\
**Post date:** [November 16, 2023, 8:03pm UTC](https://forum.opencv.org/t/fail-to-assign-value-at-y-x/15554/6 "2023-11-16T20:03:33Z")

</div>

> [@berak](#):
>
> mind challenge: try to _vectorize this_
> 
> e.g : don’t try to compare a single pixel to it’s neighbour , 2 pix right, but compare a whole Mat to another (slice), _shifted_ 2 pix to the right 😉

Interesting,  
How heavy would it be on a system compared to the algorithm above?  
This code will be embedded inside a very limited-recourse chip, so I want this to be as lightweight on the memory as possible.  
Thanks a lot 🙂

---

<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:** [November 16, 2023, 8:09pm UTC](https://forum.opencv.org/t/fail-to-assign-value-at-y-x/15554/7 "2023-11-16T20:09:30Z")

</div>

> [@amiramyss](#):
>
> as lightweight on the memory as possible.

you already allocated a vert / horz Mat for you algo, it shouldn’t be more

if you need more it’s a trade-in , mem vs compute

---

<div class="post-metadata">

**Author:** ![amiramyss](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/amiramyss/32/9101_2.png) [@amiramyss](https://forum.opencv.org/u/amiramyss)\
**Post date:** [November 16, 2023, 8:24pm UTC](https://forum.opencv.org/t/fail-to-assign-value-at-y-x/15554/8 "2023-11-16T20:24:19Z")

</div>

Thank you very much!  
Helped me a lot.
