mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
Add adaptive threshold
This commit is contained in:
parent
08aad5aac0
commit
509159ecca
@ -138,7 +138,14 @@ class Transforms(TinyObj):
|
||||
TODO:
|
||||
1:BILINEAR
|
||||
"""
|
||||
|
||||
def adaptiveThreshold(image:Image,maxval:int,subsize:int,c:int,method:int):
|
||||
"""
|
||||
AdaptiveThreshold
|
||||
method
|
||||
0:meanFilter
|
||||
1:medianFilter
|
||||
#TODO 2:gaussianFilter
|
||||
"""
|
||||
class Filter(TinyObj):
|
||||
"""The Filter class is used to
|
||||
supply some Image Filtering Algorithms ."""
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "PikaCV_Transforms.h"
|
||||
#include "PikaCV_Converter.h"
|
||||
#include "PikaCV_Filter.h"
|
||||
#include "PikaCV_common.h"
|
||||
#include "math.h"
|
||||
|
||||
@ -254,3 +255,45 @@ void PikaCV_Transforms_resize(PikaObj *self, PikaObj* image, int resizeType, int
|
||||
|
||||
#undef MAX
|
||||
#undef MIN
|
||||
|
||||
void PikaCV_Transforms_adaptiveThreshold(PikaObj *self, int c, PikaObj* image, int maxval, int method, int subsize){
|
||||
PikaCV_Image* src = obj_getStruct(image, "image");
|
||||
|
||||
if (NULL == src) {
|
||||
pika_assert(0);
|
||||
return;
|
||||
}
|
||||
if(c<-255||c>255){
|
||||
pika_assert(0);
|
||||
return;
|
||||
}
|
||||
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
||||
PikaCV_Converter_toGray(self,image);
|
||||
}
|
||||
|
||||
int size = src->size;
|
||||
uint8_t* src_data = _image_getData(image);
|
||||
|
||||
switch (method)
|
||||
{
|
||||
case 0:
|
||||
PikaCV_Filter_meanFilter(self,image,subsize,subsize); //均值滤波
|
||||
break;
|
||||
case 1:
|
||||
PikaCV_Filter_medianFilter(self,image); //中值滤波
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
uint8_t* smooth_data = _image_getData(image);
|
||||
for(int i=0;i<size;i++){
|
||||
smooth_data[i] -= c;
|
||||
}
|
||||
for(int i=0;i<size;i++){
|
||||
src_data[i] = src_data[i] > smooth_data[i] ? maxval : 0 ;
|
||||
}
|
||||
|
||||
obj_setBytes(image, "_data", src_data, src->size);
|
||||
|
||||
}
|
||||
|
@ -138,7 +138,14 @@ class Transforms(TinyObj):
|
||||
TODO:
|
||||
1:BILINEAR
|
||||
"""
|
||||
|
||||
def adaptiveThreshold(image:Image,maxval:int,subsize:int,c:int,method:int):
|
||||
"""
|
||||
AdaptiveThreshold
|
||||
method
|
||||
0:meanFilter
|
||||
1:medianFilter
|
||||
#TODO 2:gaussianFilter
|
||||
"""
|
||||
class Filter(TinyObj):
|
||||
"""The Filter class is used to
|
||||
supply some Image Filtering Algorithms ."""
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "PikaCV_Transforms.h"
|
||||
#include "PikaCV_Converter.h"
|
||||
#include "PikaCV_Filter.h"
|
||||
#include "PikaCV_common.h"
|
||||
#include "math.h"
|
||||
|
||||
@ -254,3 +255,45 @@ void PikaCV_Transforms_resize(PikaObj *self, PikaObj* image, int resizeType, int
|
||||
|
||||
#undef MAX
|
||||
#undef MIN
|
||||
|
||||
void PikaCV_Transforms_adaptiveThreshold(PikaObj *self, int c, PikaObj* image, int maxval, int method, int subsize){
|
||||
PikaCV_Image* src = obj_getStruct(image, "image");
|
||||
|
||||
if (NULL == src) {
|
||||
pika_assert(0);
|
||||
return;
|
||||
}
|
||||
if(c<-255||c>255){
|
||||
pika_assert(0);
|
||||
return;
|
||||
}
|
||||
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
||||
PikaCV_Converter_toGray(self,image);
|
||||
}
|
||||
|
||||
int size = src->size;
|
||||
uint8_t* src_data = _image_getData(image);
|
||||
|
||||
switch (method)
|
||||
{
|
||||
case 0:
|
||||
PikaCV_Filter_meanFilter(self,image,subsize,subsize); //均值滤波
|
||||
break;
|
||||
case 1:
|
||||
PikaCV_Filter_medianFilter(self,image); //中值滤波
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
uint8_t* smooth_data = _image_getData(image);
|
||||
for(int i=0;i<size;i++){
|
||||
smooth_data[i] -= c;
|
||||
}
|
||||
for(int i=0;i<size;i++){
|
||||
src_data[i] = src_data[i] > smooth_data[i] ? maxval : 0 ;
|
||||
}
|
||||
|
||||
obj_setBytes(image, "_data", src_data, src->size);
|
||||
|
||||
}
|
||||
|
@ -132,5 +132,20 @@ TEST(PikaCV, test9) {
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(PikaCV, test10) {
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
/* run */
|
||||
__platform_printf("BEGIN\r\n");
|
||||
pikaVM_runSingleFile(pikaMain, "test/python/PikaCV/PikaCV_test10.py");
|
||||
/* collect */
|
||||
/* assert */
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
7
port/linux/test/python/PikaCV/PikaCV_test10.py
Normal file
7
port/linux/test/python/PikaCV/PikaCV_test10.py
Normal file
@ -0,0 +1,7 @@
|
||||
import PikaCV as cv
|
||||
img = cv.Image()
|
||||
img.read("test/assets/test.jpg")
|
||||
cv.Converter.toGray(img)
|
||||
cv.Transforms.adaptiveThreshold(img,255,3,0,0)
|
||||
cv.Converter.toBMP(img)
|
||||
img.write("test/out/test10.bmp")
|
Loading…
x
Reference in New Issue
Block a user