# Reverse Pixel Selection OpenCV

**URL:** <https://discourse.processing.org/t/reverse-pixel-selection-opencv/17918>\
**Category:** Libraries\
**Created:** [February 18, 2020, 7:19pm UTC](https://discourse.processing.org/t/reverse-pixel-selection-opencv/17918 "2020-02-18T19:19:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ElwoodBlues](https://avatars.discourse-cdn.com/v4/letter/e/cc9497/32.png) [@ElwoodBlues](https://discourse.processing.org/u/ElwoodBlues)\
**Post date:** [February 18, 2020, 7:19pm UTC](https://discourse.processing.org/t/reverse-pixel-selection-opencv/17918/1 "2020-02-18T19:19:24Z")

</div>

Hello, I have a sketch working to pixelate faces with opencv, however I would like to reverse this, ie. pixelate the background and only have the faces showing as the normal video feed. Is there a way that I can mask out the selected area of pixels for the face from the pixelation? Any advice would be greatly appreciated, my code is below.

```auto
1. import processing.video.*;
2. import gab.opencv.*;
3. import java.awt.Rectangle;

4. int blockSize = 25;

5. Capture video;
6. OpenCV opencv;
7. Rectangle[] faces;
8. PImage frame;

9. void setup() {
10. size(640, 480);
11. noStroke();
12. String[] cameras = Capture.list();
13. video = new Capture(this, cameras[0]);
14. opencv = new OpenCV(this, width, height);
15. opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE); 
16. faces = opencv.detect();
17. frame = createImage(width, height, RGB);
18. frameRate(24.0);
19. video.start();
20. }

21. void draw() {

22. if (video.available() == true)
23. {
24. video.read();
25. frame = video.copy();
26. frame.resize(width, height);
27. image(frame, 0, 0);
28. opencv.loadImage(frame);
29. }
30. faces = opencv.detect(1.2, 3, 0, 40, 500); //adjust last two numbers to give min and max for face size
31. for (int i = 0; i < faces.length; i++) {
32. int x = (faces[i].x * blockSize) / width ;
33. int y = (faces[i].y * blockSize) / height -1;
34. int w = (faces[i].width / blockSize);
35. int h = (faces[i].height / blockSize);
36. pixelate(x + 1, y, w, h+2);
37. }
38. }

39. void pixelate(int startX, int startY, int widthX, int heightY) {
40. frame.loadPixels();

41. for (int y = startY; y < startY + heightY; y++) 
42. {
43. for (int x = startX; x < startX + widthX; x++) {

44. fill(frame.get(x*blockSize, y*blockSize));
45. rect(x*blockSize, y*blockSize, blockSize, blockSize);
46. }
47. }
48. }

```
