If facesArray.Length>0 Then 'We are only interested in first detection
Dim drawRectangle As Boolean=True '<-- define this here or in Sub Globals if you want a bounding rectangle around the detected face.
If drawRectangle=True Then
Dim myColorScalar As OCVScalar
myColorScalar.Initialize4(0,255,0,255) 'Order is R,G,B,A. In this case we are drawing a green rectangle
' We draw a green rectangle (with line thickness=4). As facesArray() already holds the detected faces rectangles, just need
' the top-left (tl) and bottom-right (br) points to draw it
mImgProc.rectangle1(mRgba,facesArray(0).tl,facesArray(0).br,myColorScalar,4)
Else '<--- original code (glasses)
'The detected face rectangle size is: W_face x H_face
Dim W_glasses As Int = facesArray(0).width ' Will rescale our original glasses Mat width to W_face,
Dim H_glasses As Int = W_glasses/3 ' keep its original aspect ratio (3:1)
Dim mScaledGlasses As OCVMat
mScaledGlasses.Initialize
Dim scaledGlassesSize As OCVSize
scaledGlassesSize.Initialize2( Array As Double( W_glasses,H_glasses))
mImgProc.resize1( mOriginalGLasses, mScaledGlasses,scaledGlassesSize)
'Now apply the effect to the full color image.
Dim mBGRA_ROI As OCVMat = mRgba.submat( _ 'This does NOT create a copy, but a reference to a region of interest
facesArray(0).tl.y + facesArray(0).height/4, _
facesArray(0).tl.y + facesArray(0).height/4+H_glasses, _
facesArray(0).tl.x, _
facesArray(0).br.x)
' We want to copy the glasses to the selected region of the face
' As the square containing them has some transparent pixels, we use them as a mask
Dim glassesMask As OCVMat
glassesMask.Initialize
mCore.extractChannel(mScaledGlasses,glassesMask,3) 'Use alpha (channel 3) from overlay image to define the mask to be used for copy
mScaledGlasses.copyTo1(mBGRA_ROI, glassesMask)
End If
End if