Skip to content

Commit

Permalink
Mat properties, reading files and converting type
Browse files Browse the repository at this point in the history
- Mat
 -- get_rows
 -- get_cols
- Imread
- convertTo
- Cleaning some of the code
- Test Image on Demo project
- Missing the definitions of the constants for conversion...
  • Loading branch information
RafaDdS committed May 1, 2024
1 parent e40e2c4 commit 194dfb0
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 50 deletions.
10 changes: 7 additions & 3 deletions Demo/OpenCVGodot.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extends OpenCVGodot

@onready var sprite = $Sprite
@onready var sprite2 = $Sprite2
@onready var sprite3 = $Sprite3

var pic:Mat;
# Called when the node enters the scene tree for the first time.
Expand All @@ -11,13 +12,16 @@ func _ready():

sprite.texture = tex


var f = OpenCVGodot.imread("image.png")
sprite3.texture = ImageTexture.create_from_image(f.get_image())

print(Mat.new().get_rows(), ", ", Mat.new().get_cols())


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
var mat = OpenCVGodot.take_picture()


mat.convert_to(5)
var p = OpenCVGodot.sqrt(mat)
var tex2: ImageTexture = ImageTexture.create_from_image(p.get_image())
sprite2.texture = tex2
Binary file added Demo/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions Demo/image.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://vlf01672lxxf"
path="res://.godot/imported/image.png-2de165adb17dfebcee8a7cd6c9833936.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://image.png"
dest_files=["res://.godot/imported/image.png-2de165adb17dfebcee8a7cd6c9833936.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
114 changes: 72 additions & 42 deletions src/OpenCVGodot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,7 @@ using namespace godot;

Mat::Mat()
{
if ( image == nullptr )
{
image = Ref<Image>( memnew( Image ) );
}
}

Mat::Mat( String path )
{
if ( image == nullptr )
{
image = Ref<Image>( memnew( Image ) );
}
image = Ref<Image>( memnew( Image ) );
}

Mat::~Mat()
Expand All @@ -43,37 +32,23 @@ Mat::~Mat()
}
}

void Mat::content()
{
}

Ref<Image> Mat::get_image()
{
if ( image.is_null() || image->is_empty() )
{
Array *array = memnew( Array );

uint8_t *pixelPtr = (uint8_t *)mat.data;
int cn = mat.channels();
cv::Scalar_<uint8_t> bgrPixel;

for ( int i = 0; i < mat.rows; i++ )
{
for ( int j = 0; j < mat.cols; j++ )
{
uint8_t B = pixelPtr[i * mat.cols * cn + j * cn + 0]; // B
uint8_t G = pixelPtr[i * mat.cols * cn + j * cn + 1]; // G
uint8_t R = pixelPtr[i * mat.cols * cn + j * cn + 2]; // R

// do something with BGR values...
array->append( R );
array->append( G );
array->append( B );
}
}

image = Image::create_from_data( mat.cols, mat.rows, false, Image::Format::FORMAT_RGB8,
PackedByteArray( *array ) );
cv::Mat rgbMat;

cv::cvtColor( mat, rgbMat, cv::COLOR_BGR2RGB );
rgbMat.convertTo( rgbMat, CV_8U );

int sizear = rgbMat.cols * rgbMat.rows * rgbMat.channels();

PackedByteArray bytes;
bytes.resize( sizear );
memcpy( bytes.ptrw(), rgbMat.data, sizear );

image = Image::create_from_data( rgbMat.cols, rgbMat.rows, false,
Image::Format::FORMAT_RGB8, bytes );
}

return image;
Expand All @@ -85,22 +60,45 @@ void Mat::set_image( Ref<Image> _image )
image = _image;
}

void Mat::set_mat( cv::Mat _mat )
int Mat::get_rows()
{
return mat.rows;
}

int Mat::get_cols()
{
return mat.cols;
}

void Mat::convert_to( int rtype )
{
try
{
mat.convertTo( mat, rtype );
}
catch ( cv::Exception &e )
{
UtilityFunctions::push_error( e.what() );
}
}

void Mat::set_mat( cv::Mat _mat )
{
mat = _mat;
}

cv::Mat Mat::get_mat()
{

return mat;
}

void Mat::_bind_methods()
{
ClassDB::bind_method( D_METHOD( "get_image" ), &Mat::get_image );
ClassDB::bind_method( D_METHOD( "set_image", "Image" ), &Mat::set_image );
ClassDB::bind_method( D_METHOD( "get_rows" ), &Mat::get_rows );
ClassDB::bind_method( D_METHOD( "get_cols" ), &Mat::get_cols );
ClassDB::bind_method( D_METHOD( "convert_to" ), &Mat::convert_to );
}

OpenCVGodot::OpenCVGodot()
Expand Down Expand Up @@ -223,7 +221,14 @@ Ref<Mat> OpenCVGodot::bitwise_not( Ref<Mat> mat, Ref<Mat> mask )
mask = Ref<Mat>( memnew( Mat ) );
}

cv::bitwise_not( mat->get_mat(), outMat, mask->get_mat() );
try
{
cv::bitwise_not( mat->get_mat(), outMat, mask->get_mat() );
}
catch ( cv::Exception &e )
{
UtilityFunctions::push_error( e.what() );
}

output->set_mat( outMat );

Expand Down Expand Up @@ -302,6 +307,27 @@ Ref<Mat> OpenCVGodot::mat_in_mat_out_wrapper( void ( *func )( cv::InputArray, cv
return output;
}

// Imgcodecs
Ref<Mat> OpenCVGodot::imread( String filename )
{
cv::Mat outMat;
Ref<Mat> output = Ref<Mat>( memnew( Mat ) );

try
{
const char *path = filename.utf8().get_data();
outMat = cv::imread( path );
}
catch ( cv::Exception &e )
{
UtilityFunctions::push_error( e.what() );
}

output->set_mat( outMat );

return output;
}

// Helper methods
Ref<Mat> OpenCVGodot::take_picture()
{
Expand Down Expand Up @@ -370,4 +396,8 @@ void OpenCVGodot::_bind_methods()
ClassDB::bind_static_method( "OpenCVGodot", D_METHOD( "sqrt" ), &OpenCVGodot::sqrt, "mat" );
ClassDB::bind_static_method( "OpenCVGodot", D_METHOD( "transpose" ), &OpenCVGodot::transpose,
"mat" );

// Imgcodecs
ClassDB::bind_static_method( "OpenCVGodot", D_METHOD( "imread" ), &OpenCVGodot::imread,
"filename" );
}
9 changes: 7 additions & 2 deletions src/OpenCVGodot.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ class Mat : public RefCounted

public:
Mat();
Mat( String path );
~Mat();

Ref<Image> image;
Ref<Image> get_image();
void set_image( Ref<Image> _image );

int get_rows();
int get_cols();

cv::Mat get_mat();
void set_mat( cv::Mat _mat );

void content();
void convert_to( int rtype );

cv::Mat mat;
};
Expand Down Expand Up @@ -68,6 +70,9 @@ class OpenCVGodot : public Node
static Ref<Mat> sqrt( Ref<Mat> mat );
static Ref<Mat> transpose( Ref<Mat> mat );

// Imgcodecs
static Ref<Mat> imread( String filename );

protected:
static void _bind_methods();

Expand Down
1 change: 0 additions & 1 deletion src/RegisterExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace

godot::ClassDB::register_class<Mat>();
godot::ClassDB::register_class<OpenCVGodot>();
godot::ClassDB::register_class<GDExtensionTemplate>();
}

/// @brief Called by Godot to let us do any cleanup.
Expand Down
7 changes: 5 additions & 2 deletions support_files/icons/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 194dfb0

Please sign in to comment.