官术网_书友最值得收藏!

  • OpenCV 4 with Python Blueprints
  • Dr. Menua Gevorgyan Arsen Mamikonyan Michael Beyeler
  • 421字
  • 2021-06-24 16:50:01

Drafting a custom filter layout

Now we are almost done! If we want to use the BaseLayout class, we need to provide code for the two methods that were previously left blank, which are as follows:

  • augment_layout: This is where we can make task-specific modifications to the GUI layout.
  • process_frame: This is where we perform task-specific processing on each captured frame of the camera feed.

We also need to change the constructor to initialize any parameters we will needin this case, the canvas background for the pencil sketch:

    def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
color_canvas = load_img_resized('pencilsketch_bg.jpg',
(self.imgWidth, self.imgHeight))
self.canvas = cv2.cvtColor(color_canvas, cv2.COLOR_RGB2GRAY)

To customize the layout, we arrange a number of radio buttons horizontallyone button per image effect mode. Here, the style=wx.RB_GROUP option makes sure that only one of radio buttons can be selected at a time. And to make these changes visible, pnl needs to be added to a list of existing panelsself.panels_vertical:

    def augment_layout(self):
""" Add a row of radio buttons below the camera feed. """

# create a horizontal layout with all filter modes as radio buttons
pnl = wx.Panel(self, -1)
self.mode_warm = wx.RadioButton(pnl, -1, 'Warming Filter', (10, 10),
style=wx.RB_GROUP)
self.mode_cool = wx.RadioButton(pnl, -1, 'Cooling Filter', (10, 10))
self.mode_sketch = wx.RadioButton(pnl, -1, 'Pencil Sketch', (10, 10))
self.mode_cartoon = wx.RadioButton(pnl, -1, 'Cartoon', (10, 10))
hbox = wx.BoxSizer(wx.HORIZONTAL)
hbox.Add(self.mode_warm, 1)
hbox.Add(self.mode_cool, 1)
hbox.Add(self.mode_sketch, 1)
hbox.Add(self.mode_cartoon, 1)
pnl.SetSizer(hbox)

# add panel with radio buttons to existing panels in a vertical
# arrangement
self.panels_vertical.Add(pnl, flag=wx.EXPAND | wx.BOTTOM | wx.TOP,
border=1

The last method to be specified is process_frame. Recall that this method is triggered whenever a new camera frame is received. All that we need to do is pick the right image effect to be applied, which depends on the radio button configuration. We simply check which of the buttons is currently selected and call the corresponding render method:

    def process_frame(self, frame_rgb: np.ndarray) -> np.ndarray:
"""Process the frame of the camera (or other capture device)

Choose a filter effect based on the which of the radio buttons
was clicked.

:param frame_rgb: Image to process in rgb format, of shape (H, W, 3)
:return: Processed image in rgb format, of shape (H, W, 3)
"""
if self.mode_warm.GetValue():
return self._render_warm(frame_rgb)
elif self.mode_cool.GetValue():
return self._render_cool(frame_rgb)
elif self.mode_sketch.GetValue():
return pencil_sketch_on_canvas(frame_rgb, canvas=self.canvas)
elif self.mode_cartoon.GetValue():
return cartoonize(frame_rgb)
else:
raise NotImplementedError()

And we're done! The following screenshot shows us the output pictures with different filters:

The preceding screenshot shows all of the four filters that we created applied to a single image.

主站蜘蛛池模板: 宝丰县| 益阳市| 海丰县| 黑龙江省| 姜堰市| 鞍山市| 广德县| 桓仁| 突泉县| 乌鲁木齐县| 灵台县| 顺义区| 黄平县| 渭源县| 益阳市| 肃宁县| 高台县| 离岛区| 黑龙江省| 马龙县| 呼图壁县| 赣榆县| 平果县| 永康市| 石嘴山市| 高尔夫| 台中县| 东乌珠穆沁旗| 江华| 枣阳市| 张家港市| 宜兰县| 林州市| 慈利县| 务川| 福州市| 息烽县| 景宁| 拜城县| 双流县| 彰化县|