Hello everyone, can anyone point me in the direction of how to identify if a certain color exists for example red by returning true or false? This is the code I'm trying to modify.
What is the function that scans for an image from pixel 0,0?
any help is much appreciated, thanks.
B4X:
#Region Project Attributes
#ApplicationLabel: OCV Image Manipulations
#VersionCode: 2
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim NUM_IMAGES As Int =1
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim ocl As OCVOpenCVLoader
Dim names(NUM_IMAGES) As String = Array As String("test1.jpg")
Dim srcIV(NUM_IMAGES),dstIV As ImageView
Dim srcBitmap(NUM_IMAGES),dstBitmap As Bitmap
Dim srcMat,tmpMat,tmpMatGray,dstMat As OCVMat
Dim selectedIndex As Int=0 'by default, select first picture
Dim optionCaption(10) As String = Array As String("HFlip","VFlip","color swap","Blur","Draw marker","Canny","dilate","erode","circles","Detect color red")
Dim optionChecked(10) As Boolean = Array As Boolean(False,False,False,False,False,False,False,False,False,False)
Dim mCore As OCVCore
Dim mUtils As OCVUtils
Dim mImgProc As OCVImgproc
Dim mCvType As OCVCvType
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
'--------------------------------------
'Prepare layout
'--------------------------------------
'Load our test pics
Dim k As Int
For k=0 To NUM_IMAGES-1
srcBitmap(k).Initialize(File.DirAssets,names(k))'"test"&k&".jpg") 'Image source: https://wallpaperbrowse.com/media/images/Images-3.jpg
srcIV(k).Initialize("srcIV")
srcIV(k).Gravity=Gravity.FILL
srcIV(k).Tag=k
Activity.AddView(srcIV(k),k*(120dip),0,100dip,75dip)
srcIV(k).Bitmap=srcBitmap(k)
Next
dstIV.Initialize("")
dstIV.Gravity=Gravity.FILL
Activity.AddView(dstIV, 2dip,100dip,100%X, 100%X*480/640 ) 'keep aspect ratio.
dstBitmap.InitializeMutable(640,480)
dstIV.Bitmap = dstBitmap
'Options
Dim chkOptions(10) As CheckBox
Dim k As Int
For k=0 To chkOptions.Length-1
Log(k)
chkOptions(k).Initialize("chkOptions")
chkOptions(k).Tag = k
chkOptions(k).Text = optionCaption(k)
Activity.AddView( chkOptions(k), 20dip + 160dip*Floor(k/5) , 365dip + 30dip*(k Mod 5), 140dip, 30dip)
Next
mUtils.bitmapToMat1(srcBitmap(selectedIndex),srcMat)
End Sub
Sub srcIV_Click
Dim IV As ImageView=Sender
Dim index As Int = IV.Tag
If index>=0 And index<NUM_IMAGES Then
selectedIndex = index
mUtils.bitmapToMat1(srcBitmap(selectedIndex),srcMat)
CallSubDelayed(Me,"ProcessImage")
End If
End Sub
Sub chkOptions_CheckedChange(Checked As Boolean)
Dim chk As CheckBox = Sender
Dim index As Int = chk.tag
If index>=0 And index<10 Then
optionChecked(index) = chk.Checked
CallSubDelayed(Me,"ProcessImage")
End If
End Sub
Sub ProcessImage
srcMat.copyTo( tmpMat) ' Clone source
'============================================================================================
' Apply different operations based on selected options
'============================================================================================
If optionChecked(0)=True Then mCore.flip(tmpMat, tmpMat, 1) ' FLip horizontally
If optionChecked(1)=True Then mCore.flip(tmpMat, tmpMat, 0) ' FLip vertically
If optionChecked(2)=True Then mImgProc.cvtColor1(tmpMat,tmpMat,mImgProc.COLOR_RGBA2BGRA) ' Color swap
If optionChecked(3)=True Then ' Blur
Dim mSize As OCVSize
mSize.Initialize2(Array As Double(5,5))
mImgProc.blur2(tmpMat,tmpMat,mSize)
End If
If optionChecked(4)=True Then ' Draw marker
Dim myPos As OCVPoint
myPos.Initialize( tmpMat.cols()/2, tmpMat.rows()/2)
Dim myColorScalar As OCVScalar
myColorScalar.Initialize3(0,255,0)
mImgProc.drawMarker1(tmpMat,myPos,myColorScalar)
End If
If optionChecked(5)=True Then ' Canny
mImgProc.cvtColor1(tmpMat,tmpMat,mImgProc.COLOR_RGBA2GRAY)
mImgProc.Canny3(tmpMat,tmpMat,50,150) 'Change thresholds according to desired results. A good rule of thumb to start is that th2 = 3*th1
End If
If optionChecked(6)=True Then
Dim mKernel As OCVMat
mKernel.Initialize2(5,5, mCvType.CV_8UC1)
mImgProc.dilate2(tmpMat,tmpMat,mKernel) ' Dilate
End If
If optionChecked(7)=True Then ' Erode
Dim mKernel As OCVMat
mKernel.Initialize2(5,5, mCvType.CV_8UC1)
mImgProc.erode2(tmpMat,tmpMat,mKernel)
End If
If optionChecked(8)=True Then
' If option8 is enabled, te other ones don't have any effect (these two lines get again the original image)
srcMat.copyTo( tmpMat)
mImgProc.cvtColor1(tmpMat,tmpMatGray,mImgProc.COLOR_RGBA2GRAY)
'HoughCircles --> function reference: http://docs.opencv.org/2.4/modules/imgproc/doc/feature_detection.html?highlight=houghcircles#houghcircles
Dim myColorScalar As OCVScalar
myColorScalar.Initialize3(0,255,0)
Dim circles As OCVMatOfPoint3f
circles.Initialize
'dp=1 --> scaling factor of the algorith. 1=start with the original image
'minDistance --> to prevent false positives. But if circles are concentrical, leave this factor as small as possible
'mImgProc.HoughCircles1( tmpMatGray,circles,mImgProc.HOUGH_GRADIENT,1,10)
mImgProc.HoughCircles(tmpMatGray,circles,mImgProc.HOUGH_GRADIENT,1.0d,10,150,100,20,240)
'The function HoughCircles returns an array of 3-points (x,y,radius)
Log(circles.rows&" "&circles.cols&" "&circles.depth)
Dim allCircles() As OCVPoint3= circles.toArray()
For k=0 To allCircles.Length-1
Log("Detected circle: x="&allCircles(k).x &" y="& allCircles(k).y &" radius="& allCircles(k).z)
'We draw a colored circle with those coordinates
Dim xyPoint As OCVPoint
xyPoint.Initialize(allCircles(k).x,allCircles(k).y)
mImgProc.circle2(tmpMat,xyPoint,allCircles(k).z,myColorScalar)
Next
End If
If optionChecked(9)=True Then ' Erode
Log("checkbox9")
End If
'============================================================================================
' Convert Mat to Bitmap
'============================================================================================
mUtils.matToBitmap1(tmpMat,dstBitmap)
dstIV.invalidate
End Sub
Sub Activity_Resume
ProcessImage
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
' Not added to the layout. But we can easily save any OCVMat after converting it to bitmap
#If 0
Sub btnSave_Click
SaveBitmapToFile(dstBitmap,File.DirRootExternal,"processedbitmap.jpg")
End Sub
Sub SaveBitmapToFile(b As Bitmap, myDir As String, myFilename As String)
Dim Out As OutputStream = File.OpenOutput(myDir, myFilename,False)
b.WriteToStream(Out,100,"JPEG")
Out.Flush
Out.Close
End Sub
#End If
any help is much appreciated, thanks.
Attachments
Last edited: