# Problems creating a chroma key without mask on C++

**URL:** <https://forum.opencv.org/t/problems-creating-a-chroma-key-without-mask-on-c/2009>\
**Category:** C++\
**Tags:** mat, imgproc, core\
**Created:** [March 7, 2021, 12:11pm UTC](https://forum.opencv.org/t/problems-creating-a-chroma-key-without-mask-on-c/2009 "2021-03-07T12:11:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rewcord](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/rewcord/32/1288_2.png) [@Rewcord](https://forum.opencv.org/u/Rewcord)\
**Post date:** [March 7, 2021, 12:11pm UTC](https://forum.opencv.org/t/problems-creating-a-chroma-key-without-mask-on-c/2009/1 "2021-03-07T12:11:09Z")

</div>

Hi. I’m a student of Computer Science, and I’m doing a project in which i must to do a chroma key on a real time video, using RGB byte vectors. I have develop these code but did not work.

void CET21CanalAlphaDlg::OnBnClickedChroma()  
{  
for (int y = 0; y \< frame.rows;y++) {  
for (int x = 0;x \< frame.cols;x++) {

```
		Vec3b& bgra = frame.at<Vec3b>(y, x);
		bgra[0] = 0;
		bgra[1] = 0;
		bgra[2] = 0;
		frame.at<Vec3b>(y, x) = bgra;
	}
}

```

}

I’m trying to see the all pixels of the video black, in order to have a first approach to the final result.

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [March 7, 2021, 3:54pm UTC](https://forum.opencv.org/t/problems-creating-a-chroma-key-without-mask-on-c/2009/2 "2021-03-07T15:54:48Z")

</div>

Hi,  
Why do you mean by it does not work?  
Vec3b is three byte. Are you sure that frame got three channel?

I can replace your code with one line  
`frame.setTo(bgra);`

Don’t use loop to access pixel

---

<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:** [March 7, 2021, 5:04pm UTC](https://forum.opencv.org/t/problems-creating-a-chroma-key-without-mask-on-c/2009/3 "2021-03-07T17:04:01Z")

</div>

> [@laurent.berger](#):
>
> Don’t use loop to access pixel

you _can_ but it’s a bad idea because OpenCV gives you means that are faster (`.setTo` as mentioned), so you can avoid writing your own loops.

---

<div class="post-metadata">

**Author:** ![Rewcord](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/rewcord/32/1288_2.png) [@Rewcord](https://forum.opencv.org/u/Rewcord)\
**Post date:** [March 7, 2021, 9:29pm UTC](https://forum.opencv.org/t/problems-creating-a-chroma-key-without-mask-on-c/2009/4 "2021-03-07T21:29:11Z")

</div>

> [@crackwitz](#):
>
> you _can_ but it’s a bad idea because OpenCV gives you means that are faster ( `.setTo` as mentioned), so you can avoid writing your own loops.

I tried also that, but it do not work, I mean as I am working with my camera, when I use the setTo function do not show any change. That’s the code for showing the camera:

void CET21CanalAlphaDlg::OnTimer(UINT\_PTR nIDEvent)  
{  
if (cap.isOpened()) {  
cap.read(frame);  
imshow(“Live”, frame);  
if (videoOut.isOpened()) {

```
			videoOut.write(frame);
		}
	
}
CDialogEx::OnTimer(nIDEvent);

```

}

and

void CET21CanalAlphaDlg::OnBnClickedOk2()  
{  
UpdateData(TRUE);

```
if (!m_bCapturando) {
	if (archivovideo.IsEmpty()) {
		cap.open(idcamara);
	}
	else {
		std::string ss;
		ss = CW2A((LPCTSTR)archivovideo);
		cap.open(ss);
	}
	if (!cap.isOpened()) {
		AfxMessageBox(_T("Imposible abrir camara o video"), MB_OK);
		return;
	}
	SetTimer(1, (uint)(10000.0 / fps), NULL);
	GetDlgItem(IDOK2)->SetWindowTextW(_T("Parar"));
	m_bCapturando = true;
}
else {
	KillTimer(1);
	GetDlgItem(IDOK2)->SetWindowTextW(_T("Capturar"));
	m_bCapturando = false;
}

```

}
