diff --git a/port/linux/package/pikascript/PikaCV.pyi b/port/linux/package/pikascript/PikaCV.pyi index a1823f232..6c2fa362c 100644 --- a/port/linux/package/pikascript/PikaCV.pyi +++ b/port/linux/package/pikascript/PikaCV.pyi @@ -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""" \ No newline at end of file diff --git a/port/linux/package/pikascript/pikascript-lib/PikaCV/PikaCV_Transforms.c b/port/linux/package/pikascript/pikascript-lib/PikaCV/PikaCV_Transforms.c index 693c9577f..cd8475a43 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaCV/PikaCV_Transforms.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaCV/PikaCV_Transforms.c @@ -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); + +} \ No newline at end of file diff --git a/port/linux/test/PikaCV-test.cpp b/port/linux/test/PikaCV-test.cpp index 5d66f1e56..915afc50c 100644 --- a/port/linux/test/PikaCV-test.cpp +++ b/port/linux/test/PikaCV-test.cpp @@ -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); } \ No newline at end of file diff --git a/port/linux/test/python/PikaCV/PikaCV_test4.py b/port/linux/test/python/PikaCV/PikaCV_test4.py new file mode 100644 index 000000000..e77ce787c --- /dev/null +++ b/port/linux/test/python/PikaCV/PikaCV_test4.py @@ -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") + diff --git a/port/linux/test/python/PikaCV/PikaCV_test5.py b/port/linux/test/python/PikaCV/PikaCV_test5.py new file mode 100644 index 000000000..16dfd6763 --- /dev/null +++ b/port/linux/test/python/PikaCV/PikaCV_test5.py @@ -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")