Add setROI and threshold in PikaCV

This commit is contained in:
purewind7 2022-07-18 16:00:15 +08:00 committed by 李昂
parent 4ca4c4ac52
commit 76aaf5c63b
5 changed files with 141 additions and 0 deletions

View File

@ -116,3 +116,13 @@ class Transforms(TinyObj):
@staticmethod
def rotateDown(image: Image):
"""Rotate the image """
def threshold(image:Image,thre:int,maxval:int,thresholdType:int):
"""
0:THRESH_BINARY
1:THRESH_BINARY_INV
2:THRESH_TRUNC
3:THRESH_TOZERO
4:THRESH_TOZERO_INV
"""
def setROI(image:Image,x:int,y:int,w:int,h:int) :
"""xywh"""

View File

@ -34,3 +34,88 @@ void PikaCV_Transforms_rotateDown(PikaObj* self, PikaObj* image) {
img->size = size_new;
arg_deinit(arg_data_new);
}
void PikaCV_Transforms_threshold(PikaObj *self, PikaObj* image, int maxval, int thre, int thresholdType){
PikaCV_Image* src = obj_getStruct(image, "image");
if (NULL == src) {
pika_assert(0);
return;
}
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
PikaCV_Converter_toGray(self,image);
}
uint8_t* src_data = _image_getData(image);
int i;
if(thresholdType == 0 ){
for (i = 0; i < (src->size) ; i++) {
src_data[i] = src_data[i] > thre ? maxval : 0 ;
}
}
else if(thresholdType == 1 ){
for (i = 0; i < (src->size) ; i++) {
src_data[i] = src_data[i] > thre ? 0 : maxval ;
}
}
else if(thresholdType == 2 ){
for (i = 0; i < (src->size) ; i++) {
src_data[i] = src_data[i] > thre ? thre : src_data[i] ;
}
}
else if(thresholdType == 3 ){
for (i = 0; i < (src->size) ; i++) {
src_data[i] = src_data[i] > thre ? src_data[i] : 0 ;
}
}
else if(thresholdType == 4 ){
for (i = 0; i < (src->size) ; i++) {
src_data[i] = src_data[i] > thre ? 0 : src_data[i] ;
}
}
obj_setBytes(image, "_data", src_data, src->size);
}
void PikaCV_Transforms_setROI(PikaObj *self, int h, PikaObj* image, int w, int x, int y){
PikaCV_Image* src = obj_getStruct(image, "image");
int width = src->width;
int height = src->height;
if (NULL == src) {
pika_assert(0);
return;
}
if (src->format != PikaCV_ImageFormat_Type_RGB888) {
PikaCV_Converter_toRGB888(self,image);
}
if( x <= 0 || y <= 0 || w <= 0 || h <= 0){
pika_assert(0);
return;
}
if( x + w > width || y + h > height ){
pika_assert(0);
return;
}
int size_new = h * w * 3;
Arg* arg_data_new = arg_setBytes(NULL, "", NULL, size_new);
uint8_t* data = _image_getData(image);
uint8_t* data_new = arg_getBytes(arg_data_new);
for(int i = 0 ; i < h ; i++){
for(int j = 0 ; j < w ; j++){
data_new[i * 3 * w + j * 3] = data[ (i + y-1) * width * 3 + (j + x - 1 ) * 3 ];
data_new[i * 3 * w + j * 3 + 1] = data[ (i + y-1) * width * 3 + ( j + x - 1 ) * 3 + 1];
data_new[i * 3 * w + j * 3 + 2] = data[ (i + y-1) * width * 3 + ( j + x - 1 ) * 3 + 2];
}
}
src->height=h;
src->width=w;
src->size = size_new;
obj_setBytes(image, "_data", data_new, size_new);
arg_deinit(arg_data_new);
}

View File

@ -42,5 +42,35 @@ TEST(PikaCV, test3) {
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(PikaCV, test4) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
/* run */
__platform_printf("BEGIN\r\n");
pikaVM_runSingleFile(pikaMain, "test/python/PikaCV/PikaCV_test4.py");
/* collect */
/* assert */
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(PikaCV, test5) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
/* run */
__platform_printf("BEGIN\r\n");
pikaVM_runSingleFile(pikaMain, "test/python/PikaCV/PikaCV_test5.py");
/* collect */
/* assert */
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -0,0 +1,9 @@
import PikaCV as cv
img = cv.Image()
img.read("test/assets/test.jpg")
cv.Converter.toGray(img)
cv.Transforms.threshold(img,120,255,0)
cv.Converter.toBMP(img)
img.write("test/out/test.bmp")

View File

@ -0,0 +1,7 @@
import PikaCV as cv
img = cv.Image()
img.read("test/assets/test.jpg")
cv.Converter.toRGB888(img)
cv.Transforms.setROI(img,5,5,5,5)
cv.Converter.toBMP(img)
img.write("test/out/test1.bmp")