Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
xieguigang committed Jan 1, 2025
2 parents 943b474 + d37601d commit ce849a5
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 50 deletions.
1 change: 1 addition & 0 deletions studio/Rsharp_kit/MLkit/MLkit-netcore5.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
<ProjectReference Include="..\..\..\..\runtime\sciBASIC#\Data\word2vec\word2vec.vbproj" />
<ProjectReference Include="..\..\..\..\runtime\sciBASIC#\Data_science\DataMining\BinaryTree\btree-netcore5.vbproj" />
<ProjectReference Include="..\..\..\..\runtime\sciBASIC#\Data_science\DataMining\DataMining\DataMining.NET5.vbproj" />
<ProjectReference Include="..\..\..\..\runtime\sciBASIC#\Data_science\DataMining\DensityQuery\DensityQuery.NET5.vbproj" />
<ProjectReference Include="..\..\..\..\runtime\sciBASIC#\Data_science\DataMining\FeatureFrame\FeatureFrame.vbproj" />
<ProjectReference Include="..\..\..\..\runtime\sciBASIC#\Data_science\DataMining\hierarchical-clustering\hierarchical-clustering\hctree.NET5.vbproj" />
<ProjectReference Include="..\..\..\..\runtime\sciBASIC#\Data_science\DataMining\HMM\HMM.NET5.vbproj" />
Expand Down
163 changes: 113 additions & 50 deletions studio/Rsharp_kit/MLkit/dataset/bitmap.vb
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
#Region "Microsoft.VisualBasic::e82346187276f99f773149fe7e549efe, studio\Rsharp_kit\MLkit\dataset\bitmap.vb"

' Author:
'
' asuka ([email protected])
' xie ([email protected])
' xieguigang ([email protected])
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.



' /********************************************************************************/

' Summaries:


' Code Statistics:

' Total Lines: 224
' Code Lines: 162 (72.32%)
' Comment Lines: 30 (13.39%)
' - Xml Docs: 96.67%
'
' Blank Lines: 32 (14.29%)
' File Size: 8.83 KB


' Module bitmap_func
'
' Function: corp_rectangle, intensity_vec, open, scan_rowpeaks, summary_region
'
' Sub: Main
'
' /********************************************************************************/
' Author:
'
' asuka ([email protected])
' xie ([email protected])
' xieguigang ([email protected])
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.



' /********************************************************************************/

' Summaries:


' Code Statistics:

' Total Lines: 224
' Code Lines: 162 (72.32%)
' Comment Lines: 30 (13.39%)
' - Xml Docs: 96.67%
'
' Blank Lines: 32 (14.29%)
' File Size: 8.83 KB


' Module bitmap_func
'
' Function: corp_rectangle, intensity_vec, open, scan_rowpeaks, summary_region
'
' Sub: Main
'
' /********************************************************************************/

#End Region

Expand All @@ -58,6 +58,8 @@ Imports System.IO
Imports System.Runtime.CompilerServices
Imports Microsoft.VisualBasic.ApplicationServices.Terminal.ProgressBar
Imports Microsoft.VisualBasic.CommandLine.Reflection
Imports Microsoft.VisualBasic.DataMining.DensityQuery
Imports Microsoft.VisualBasic.Imaging
Imports Microsoft.VisualBasic.Imaging.BitmapImage
Imports Microsoft.VisualBasic.Math.Distributions
Imports Microsoft.VisualBasic.Math.SignalProcessing
Expand All @@ -82,8 +84,30 @@ Module bitmap_func

Sub Main()
Call RInternal.generic.add("summary", GetType(BitmapReader), AddressOf summary_region)
Call RInternal.Object.Converts.addHandler(GetType(SLICPixel()), AddressOf createPixelClusterTable)
End Sub

<RGenericOverloads("as.data.frame")>
Private Function createPixelClusterTable(pixels As SLICPixel(), args As list, env As Environment) As Object
Dim df As New dataframe With {
.columns = New Dictionary(Of String, Array)
}
Dim offset As Integer

Call df.add("x", From pixel As SLICPixel In pixels Select pixel.x)
Call df.add("y", From pixel As SLICPixel In pixels Select pixel.y)
Call df.add("cluster", From pixel As SLICPixel In pixels Select pixel.cluster)

For i As Integer = 0 To pixels(0).color.Length - 1
offset = i
df.add($"v{i + 1}", From pixel As SLICPixel
In pixels
Select pixel.color(offset))
Next

Return df
End Function

<RGenericOverloads("summary")>
Private Function summary_region(bmp As BitmapReader, args As list, env As Environment) As Object
Dim offset_x As Integer = args.getValue("x", env, [default]:=1)
Expand Down Expand Up @@ -202,6 +226,45 @@ Module bitmap_func
Return copy
End Function

''' <summary>
'''
''' </summary>
''' <param name="bmp"></param>
''' <returns></returns>
<ExportAPI("slic")>
<RApiReturn(GetType(SLICPixel))>
Public Function slic(bmp As Object,
Optional region_size As Double = 0.05,
Optional iterations As Integer = 1000,
Optional env As Environment = Nothing) As Object

Dim buf As BitmapBuffer

If bmp Is Nothing Then
Return RInternal.debug.stop("the given bitmap data object should not be nothing!", env)
End If

If TypeOf bmp Is Bitmap Then
buf = BitmapBuffer.FromBitmap(DirectCast(bmp, Bitmap))
ElseIf TypeOf bmp Is Image Then
buf = BitmapBuffer.FromImage(DirectCast(bmp, Image))
ElseIf TypeOf bmp Is BitmapBuffer Then
buf = bmp
Else
Return Message.InCompatibleType(GetType(BitmapBuffer), bmp.GetType, env)
End If

If region_size < 1 Then
' convert ration to pixel size
region_size = {buf.Width * region_size, buf.Height * region_size}.Average
End If

Dim method As New SLIC(buf)
Dim pixels = method.MeasureSegments(region_size, iterations)

Return pixels
End Function

''' <summary>
''' scan the peak signal inside the bitmap image data
''' </summary>
Expand Down
Binary file added test/demo/imageProcessing/demo2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/demo/imageProcessing/lena.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions test/demo/imageProcessing/slic.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
imports "bitmap" from "MLkit";

let data = readImage(file.path(@dir, "lena.jpg" ));
let regions = bitmap::slic(data,region_size = 0.3,
iterations = 10);

regions = as.data.frame(regions);

setwd(@dir);

bitmap(file = "./lena.png") {
plot(regions$x, regions$y,
class = regions$cluster,
colors = "paper",
point.size = 5,
reverse=TRUE );
};

18 changes: 18 additions & 0 deletions test/demo/imageProcessing/slic2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
imports "bitmap" from "MLkit";

let data = readImage(file.path(@dir, "/../1537192287563.jpg" ));
let regions = bitmap::slic(data,region_size = 0.3,
iterations = 10);

regions = as.data.frame(regions);

setwd(@dir);

bitmap(file = "./demo2.png") {
plot(regions$x, regions$y,
class = regions$cluster,
colors = "paper",
point.size = 5,
reverse=TRUE );
};

0 comments on commit ce849a5

Please sign in to comment.