Android Question OpenCV Crop and Zoom

advansis

Active Member
Licensed User
Longtime User
Hi guys,
I've just started to use the OpenCV3 library, greatly wrapped in Android by @JordiCP. I learned something about it, in the past, and is very powerful!
I'm trying to customize the example called "Color Blob Detection", and want to use the zoom of the camera (as a microscope), allowing to view the zoomed image in the preview panel at real-time.
I tryied to use the "resize1" function, but does not work
B4X:
mImgProc.resize1(myMat,myMat,Utils.CreateSize(320,240))
- Also tryied to create a temporary OCVMat and then assigned back to myMat: crash.
- Tried to change cameraview dimensions:
B4X:
mOpenCvCameraView.connectCamera(1280,960)
but the image becomes big, I want only a part of it...
Is there any "simple" hack to perform a x2 (or x4, x6, x8) zoom before processing the image?
Thanks in advance
 

JordiCP

Expert
Licensed User
Longtime User
Regardless of the chosen camera resolution, with OpenCV you can perform a 'digital' zoom on the preview image using Regions of Interest (ROI) and the resize method

It would be something similar to this (untested, just for reference)
B4X:
Dim m as OCVMat    '' Let's assume m contains the camera preview image, in the resolution you have chosen.

Dim mROI, mROIresized As OCVMat
Dim mROIRect As OCVRect
mROIRect.Initialize(x,y,w,h)         ' <-- the rectangular ROI that must be within the original OCVMat dimensions
mROI.Initialize7(m, mROIRect)   ' <-- Here you get  a reference to the chosen region in the original Mat
Dim mFinalSize as OCVSize
mFinalSize.initialize2( Array as double(400, 400)) '<-- The final fize

' At this moment I don't remember if mROIResized should be previously initialized, or the 'resize' sub takes care of it.
mImgProc.resize(mROI, mROIResized, mFinalSize, 0, 0, mImgProc.INTER_NEAREST) '<-- Now you'll have the chosen region with the size you want in mROIResized

Tell me if it works for you. If not, I'll be able to test it later :)
 
Upvote 0

advansis

Active Member
Licensed User
Longtime User
Thank you @JordiCP for your reply.
After the ROI computation I cannot reassign the mROIResized to myMat (that is the parameter of my xxx_newframe sub), neither with clone(nothing seems to happen), nor with copy (image remains blank).
So cannot preview my zoomed image...

My code:
B4X:
Public Sub frameprocessor_newFrame(myMat As OCVMat)
    Try
       
        Dim mROI, mROIresized As OCVMat
        Dim mROIRect As OCVRect
        mROIRect.Initialize(100,100,200,200)         ' <-- the rectangular ROI that must be within the original OCVMat dimensions
        mROI.Initialize7(myMat, mROIRect)   ' <-- Here you get  a reference to the chosen region in the original Mat
        Dim mFinalSize As OCVSize
        mFinalSize.initialize2( Array As Double(400, 400)) '<-- The final fize

        mImgProc.resize(mROI, mROIresized, mFinalSize, 0, 0, mImgProc.INTER_NEAREST)
               
        mImgProc.cvtColor1(mROIresized,myMat,mImgProc.COLOR_RGB2GRAY)
       
        ....
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
My fault, the zoomed image must be resized to the same dimensions as the original myMat

This example code works with a width/4, height/4 ROI that will change the ROI x offset on each frame.
B4X:
Public Sub frameprocessor_newFrame(myMat As OCVMat)

    xoffset = (xOffset+1) Mod (3*myMat.cols/4)        '<-- Just for test. Declare xOffset in Globals. The zoom window will move on each frame.
    Dim mROI, mROIresized As OCVMat
    Dim mROIRect As OCVRect
    mROIRect.Initialize(xOffset, 0, myMat.cols/4-1, myMat.rows/4 -1)         ' <-- the rectangular ROI that must be within the original OCVMat dimensions
    mROI.Initialize7(myMat, mROIRect)   ' <-- Here you get  a reference to the chosen region in the original Mat
   
    Dim mFinalSize As OCVSize
    mFinalSize.initialize2( Array As Double(myMat.cols, myMat.rows))      '<-- The final fize must be the same as the original
    
    mImgProc.resize(mROI.clone, myMat, mFinalSize, 0, 0, mImgProc.INTER_NEAREST)
               
End Sub
 
Upvote 0

advansis

Active Member
Licensed User
Longtime User
My fault, the zoomed image must be resized to the same dimensions as the original myMat

This example code works with a width/4, height/4 ROI that will change the ROI x offset on each frame.
B4X:
Public Sub frameprocessor_newFrame(myMat As OCVMat)

    xoffset = (xOffset+1) Mod (3*myMat.cols/4)        '<-- Just for test. Declare xOffset in Globals. The zoom window will move on each frame.
    Dim mROI, mROIresized As OCVMat
    Dim mROIRect As OCVRect
    mROIRect.Initialize(xOffset, 0, myMat.cols/4-1, myMat.rows/4 -1)         ' <-- the rectangular ROI that must be within the original OCVMat dimensions
    mROI.Initialize7(myMat, mROIRect)   ' <-- Here you get  a reference to the chosen region in the original Mat
  
    Dim mFinalSize As OCVSize
    mFinalSize.initialize2( Array As Double(myMat.cols, myMat.rows))      '<-- The final fize must be the same as the original
   
    mImgProc.resize(mROI.clone, myMat, mFinalSize, 0, 0, mImgProc.INTER_NEAREST)
              
End Sub

Thank you, @JordiCP, works like a charm!
 
Upvote 0
Top