mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
install valgrind in CI, fix valgrind err in CV
This commit is contained in:
parent
6ae8bf0c15
commit
8cc5a5a844
1
.github/workflows/CI.yml
vendored
1
.github/workflows/CI.yml
vendored
@ -23,6 +23,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
sudo apt install ninja-build
|
sudo apt install ninja-build
|
||||||
sudo apt install liblua5.3-dev
|
sudo apt install liblua5.3-dev
|
||||||
|
sudo apt install valgrind
|
||||||
|
|
||||||
- name: install_google_benchmark
|
- name: install_google_benchmark
|
||||||
run: |
|
run: |
|
||||||
|
1
.github/workflows/TEST.yml
vendored
1
.github/workflows/TEST.yml
vendored
@ -23,6 +23,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
sudo apt install ninja-build
|
sudo apt install ninja-build
|
||||||
sudo apt install liblua5.3-dev
|
sudo apt install liblua5.3-dev
|
||||||
|
sudo apt install valgrind
|
||||||
|
|
||||||
- name: install_google_benchmark
|
- name: install_google_benchmark
|
||||||
run: git clone --branch v1.6.1 https://github.com/google/benchmark.git && cd benchmark && cmake -E make_directory "build" && cmake -E chdir "build" cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../ && cmake --build "build" --config Release && sudo cmake --build "build" --config Release --target install
|
run: git clone --branch v1.6.1 https://github.com/google/benchmark.git && cd benchmark && cmake -E make_directory "build" && cmake -E chdir "build" cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../ && cmake --build "build" --config Release && sudo cmake --build "build" --config Release --target install
|
||||||
|
@ -19,7 +19,7 @@ void PikaCV_Transforms_rotateDown(PikaObj* self, PikaObj* image) {
|
|||||||
int width = img->width;
|
int width = img->width;
|
||||||
int height = img->height;
|
int height = img->height;
|
||||||
int size_new = width * height * 3;
|
int size_new = width * height * 3;
|
||||||
Arg* arg_data_new = arg_newBytes(NULL, size_new);
|
Arg* arg_data_new = arg_setBytes(NULL, "", NULL, size_new);
|
||||||
uint8_t* data = _image_getData(image);
|
uint8_t* data = _image_getData(image);
|
||||||
uint8_t* data_new = arg_getBytes(arg_data_new);
|
uint8_t* data_new = arg_getBytes(arg_data_new);
|
||||||
int i, j, k;
|
int i, j, k;
|
||||||
@ -37,7 +37,11 @@ void PikaCV_Transforms_rotateDown(PikaObj* self, PikaObj* image) {
|
|||||||
arg_deinit(arg_data_new);
|
arg_deinit(arg_data_new);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PikaCV_Transforms_threshold(PikaObj *self, PikaObj* image, int maxval, int thre, int thresholdType){
|
void PikaCV_Transforms_threshold(PikaObj* self,
|
||||||
|
PikaObj* image,
|
||||||
|
int maxval,
|
||||||
|
int thre,
|
||||||
|
int thresholdType) {
|
||||||
PikaCV_Image* src = obj_getStruct(image, "image");
|
PikaCV_Image* src = obj_getStruct(image, "image");
|
||||||
|
|
||||||
if (NULL == src) {
|
if (NULL == src) {
|
||||||
@ -45,40 +49,35 @@ void PikaCV_Transforms_threshold(PikaObj *self, PikaObj* image, int maxval, int
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
||||||
PikaCV_Converter_toGray(self,image);
|
PikaCV_Converter_toGray(self, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t* src_data = _image_getData(image);
|
uint8_t* src_data = _image_getData(image);
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
if(thresholdType == 0 ){
|
if (thresholdType == 0) {
|
||||||
for (i = 0; i < (src->size) ; i++) {
|
for (i = 0; i < (src->size); i++) {
|
||||||
src_data[i] = src_data[i] > thre ? maxval : 0 ;
|
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 == 1 ){
|
} else if (thresholdType == 2) {
|
||||||
for (i = 0; i < (src->size) ; i++) {
|
for (i = 0; i < (src->size); i++) {
|
||||||
src_data[i] = src_data[i] > thre ? 0 : maxval ;
|
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 == 2 ){
|
} else if (thresholdType == 4) {
|
||||||
for (i = 0; i < (src->size) ; i++) {
|
for (i = 0; i < (src->size); i++) {
|
||||||
src_data[i] = src_data[i] > thre ? thre : src_data[i] ;
|
src_data[i] = src_data[i] > thre ? 0 : src_data[i];
|
||||||
}
|
}
|
||||||
}
|
} else if (thresholdType == 5) {
|
||||||
else if(thresholdType == 3 ){
|
PikaCV_Transforms_setOTSU(self, image);
|
||||||
for (i = 0; i < (src->size) ; i++) {
|
} else {
|
||||||
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] ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if(thresholdType == 5 ){
|
|
||||||
PikaCV_Transforms_setOTSU(self,image);
|
|
||||||
}else{
|
|
||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -86,7 +85,12 @@ void PikaCV_Transforms_threshold(PikaObj *self, PikaObj* image, int maxval, int
|
|||||||
obj_setBytes(image, "_data", src_data, src->size);
|
obj_setBytes(image, "_data", src_data, src->size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PikaCV_Transforms_setROI(PikaObj *self, int h, PikaObj* image, int w, int x, int y){
|
void PikaCV_Transforms_setROI(PikaObj* self,
|
||||||
|
int h,
|
||||||
|
PikaObj* image,
|
||||||
|
int w,
|
||||||
|
int x,
|
||||||
|
int y) {
|
||||||
PikaCV_Image* src = obj_getStruct(image, "image");
|
PikaCV_Image* src = obj_getStruct(image, "image");
|
||||||
int width = src->width;
|
int width = src->width;
|
||||||
int height = src->height;
|
int height = src->height;
|
||||||
@ -96,27 +100,30 @@ void PikaCV_Transforms_setROI(PikaObj *self, int h, PikaObj* image, int w, int x
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (src->format != PikaCV_ImageFormat_Type_RGB888) {
|
if (src->format != PikaCV_ImageFormat_Type_RGB888) {
|
||||||
PikaCV_Converter_toRGB888(self,image);
|
PikaCV_Converter_toRGB888(self, image);
|
||||||
}
|
}
|
||||||
if( x <= 0 || y <= 0 || w <= 0 || h <= 0){
|
if (x <= 0 || y <= 0 || w <= 0 || h <= 0) {
|
||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if( x + w > width || y + h > height ){
|
if (x + w > width || y + h > height) {
|
||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int size_new = h * w * 3;
|
int size_new = h * w * 3;
|
||||||
Arg* arg_data_new = arg_newBytes(NULL, size_new);
|
Arg* arg_data_new = arg_setBytes(NULL, "", NULL, size_new);
|
||||||
uint8_t* data = _image_getData(image);
|
uint8_t* data = _image_getData(image);
|
||||||
uint8_t* data_new = arg_getBytes(arg_data_new);
|
uint8_t* data_new = arg_getBytes(arg_data_new);
|
||||||
|
|
||||||
for(int i = 0 ; i < h ; i++){
|
for (int i = 0; i < h; i++) {
|
||||||
for(int j = 0 ; j < w ; j++){
|
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] =
|
||||||
data_new[i * 3 * w + j * 3 + 1] = data[ (i + y - 1 ) * width * 3 + ( j + x - 1 ) * 3 + 1];
|
data[(i + y - 1) * width * 3 + (j + x - 1) * 3];
|
||||||
data_new[i * 3 * w + j * 3 + 2] = data[ (i + y - 1 ) * width * 3 + ( j + x - 1 ) * 3 + 2];
|
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->height = h;
|
||||||
@ -125,10 +132,9 @@ void PikaCV_Transforms_setROI(PikaObj *self, int h, PikaObj* image, int w, int x
|
|||||||
|
|
||||||
obj_setBytes(image, "_data", data_new, size_new);
|
obj_setBytes(image, "_data", data_new, size_new);
|
||||||
arg_deinit(arg_data_new);
|
arg_deinit(arg_data_new);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int PikaCV_Transforms_getOTSUthre(PikaObj *self, PikaObj* image){
|
int PikaCV_Transforms_getOTSUthre(PikaObj* self, PikaObj* image) {
|
||||||
PikaCV_Image* src = obj_getStruct(image, "image");
|
PikaCV_Image* src = obj_getStruct(image, "image");
|
||||||
|
|
||||||
if (NULL == src) {
|
if (NULL == src) {
|
||||||
@ -136,40 +142,39 @@ int PikaCV_Transforms_getOTSUthre(PikaObj *self, PikaObj* image){
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
||||||
PikaCV_Converter_toGray(self,image);
|
PikaCV_Converter_toGray(self, image);
|
||||||
}
|
}
|
||||||
const int Grayscale=256;
|
const int Grayscale = 256;
|
||||||
int width = src->width;
|
int width = src->width;
|
||||||
int height = src->height;
|
int height = src->height;
|
||||||
int size = src->size;
|
int size = src->size;
|
||||||
int threshold = 0;
|
int threshold = 0;
|
||||||
int graynum[256] = { 0 };
|
int graynum[256] = {0};
|
||||||
uint8_t* data = _image_getData(image);
|
uint8_t* data = _image_getData(image);
|
||||||
for(int i=0 ; i < size ; i++){
|
for (int i = 0; i < size; i++) {
|
||||||
graynum[data[i]]++;
|
graynum[data[i]]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
double P[256] = { 0.0 };
|
double P[256] = {0.0};
|
||||||
double PK[256] = { 0.0 };
|
double PK[256] = {0.0};
|
||||||
double MK[256] = { 0.0 };
|
double MK[256] = {0.0};
|
||||||
double srcpixnum = width*height, sumtmpPK = 0, sumtmpMK = 0;
|
double srcpixnum = width * height, sumtmpPK = 0, sumtmpMK = 0;
|
||||||
for (int i = 0; i < Grayscale; ++i){
|
for (int i = 0; i < Grayscale; ++i) {
|
||||||
P[i] = graynum[i] / srcpixnum;
|
P[i] = graynum[i] / srcpixnum;
|
||||||
PK[i] = sumtmpPK + P[i];
|
PK[i] = sumtmpPK + P[i];
|
||||||
sumtmpPK = PK[i];
|
sumtmpPK = PK[i];
|
||||||
MK[i] = sumtmpMK + i*P[i];
|
MK[i] = sumtmpMK + i * P[i];
|
||||||
sumtmpMK = MK[i];
|
sumtmpMK = MK[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
double Var=0;
|
double Var = 0;
|
||||||
for (int k = 0; k < Grayscale; ++k){
|
for (int k = 0; k < Grayscale; ++k) {
|
||||||
if(PK[k] > 0.0)
|
if (PK[k] > 0.0) {
|
||||||
{
|
double t = MK[Grayscale - 1] * PK[k] - MK[k];
|
||||||
double t = MK[Grayscale-1] * PK[k] - MK[k];
|
|
||||||
double w = PK[k] * (1 - PK[k]);
|
double w = PK[k] * (1 - PK[k]);
|
||||||
double variance = t * t / w ;
|
double variance = t * t / w;
|
||||||
if (variance > Var){
|
if (variance > Var) {
|
||||||
Var = variance ;
|
Var = variance;
|
||||||
threshold = k;
|
threshold = k;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -178,7 +183,7 @@ int PikaCV_Transforms_getOTSUthre(PikaObj *self, PikaObj* image){
|
|||||||
return threshold;
|
return threshold;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PikaCV_Transforms_setOTSU(PikaObj *self, PikaObj* image){
|
void PikaCV_Transforms_setOTSU(PikaObj* self, PikaObj* image) {
|
||||||
PikaCV_Image* src = obj_getStruct(image, "image");
|
PikaCV_Image* src = obj_getStruct(image, "image");
|
||||||
|
|
||||||
if (NULL == src) {
|
if (NULL == src) {
|
||||||
@ -186,15 +191,15 @@ void PikaCV_Transforms_setOTSU(PikaObj *self, PikaObj* image){
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
||||||
PikaCV_Converter_toGray(self,image);
|
PikaCV_Converter_toGray(self, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
int thre = PikaCV_Transforms_getOTSUthre(self,image);
|
int thre = PikaCV_Transforms_getOTSUthre(self, image);
|
||||||
uint8_t* src_data = _image_getData(image);
|
uint8_t* src_data = _image_getData(image);
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < (src->size) ; i++) {
|
for (i = 0; i < (src->size); i++) {
|
||||||
src_data[i] = src_data[i] > thre ? 255 : 0 ;
|
src_data[i] = src_data[i] > thre ? 255 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
obj_setBytes(image, "_data", src_data, src->size);
|
obj_setBytes(image, "_data", src_data, src->size);
|
||||||
@ -203,7 +208,11 @@ void PikaCV_Transforms_setOTSU(PikaObj *self, PikaObj* image){
|
|||||||
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||||
|
|
||||||
void PikaCV_Transforms_resize(PikaObj *self, PikaObj* image, int resizeType, int x, int y){
|
void PikaCV_Transforms_resize(PikaObj* self,
|
||||||
|
PikaObj* image,
|
||||||
|
int resizeType,
|
||||||
|
int x,
|
||||||
|
int y) {
|
||||||
PikaCV_Image* src = obj_getStruct(image, "image");
|
PikaCV_Image* src = obj_getStruct(image, "image");
|
||||||
int width = src->width;
|
int width = src->width;
|
||||||
int height = src->height;
|
int height = src->height;
|
||||||
@ -211,37 +220,37 @@ void PikaCV_Transforms_resize(PikaObj *self, PikaObj* image, int resizeType, int
|
|||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if( x <= 0 || y <= 0 ){
|
if (x <= 0 || y <= 0) {
|
||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (src->format != PikaCV_ImageFormat_Type_RGB888) {
|
if (src->format != PikaCV_ImageFormat_Type_RGB888) {
|
||||||
PikaCV_Converter_toRGB888(self,image);
|
PikaCV_Converter_toRGB888(self, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
int size_new = x * y * 3;
|
int size_new = x * y * 3;
|
||||||
Arg* arg_data_new = arg_newBytes(NULL, size_new);
|
Arg* arg_data_new = arg_setBytes(NULL, "", NULL, size_new);
|
||||||
uint8_t* data = _image_getData(image);
|
uint8_t* data = _image_getData(image);
|
||||||
uint8_t* data_new = arg_getBytes(arg_data_new);
|
uint8_t* data_new = arg_getBytes(arg_data_new);
|
||||||
|
|
||||||
double scale_x = (double)width / (double)x;
|
double scale_x = (double)width / (double)x;
|
||||||
double scale_y = (double)height / (double)y;
|
double scale_y = (double)height / (double)y;
|
||||||
if(resizeType == 0){
|
if (resizeType == 0) {
|
||||||
for (int i = 0; i < y ; ++i)
|
for (int i = 0; i < y; ++i) {
|
||||||
{
|
|
||||||
int sy = floor(i * scale_y);
|
int sy = floor(i * scale_y);
|
||||||
sy = MIN(sy , ( height - 1 ));
|
sy = MIN(sy, (height - 1));
|
||||||
for (int j = 0; j < x ; ++j)
|
for (int j = 0; j < x; ++j) {
|
||||||
{
|
|
||||||
int sx = floor(j * scale_x);
|
int sx = floor(j * scale_x);
|
||||||
sx = MIN(sx , ( width - 1 ));
|
sx = MIN(sx, (width - 1));
|
||||||
|
|
||||||
data_new[i * x * 3 + j * 3]=data[sy * width * 3 + sx * 3];
|
data_new[i * x * 3 + j * 3] = data[sy * width * 3 + sx * 3];
|
||||||
data_new[i * x * 3 + j * 3 + 1]=data[sy * width * 3 + sx * 3 + 1];
|
data_new[i * x * 3 + j * 3 + 1] =
|
||||||
data_new[i * x * 3 + j * 3 + 2]=data[sy * width * 3 + sx * 3 + 2];
|
data[sy * width * 3 + sx * 3 + 1];
|
||||||
|
data_new[i * x * 3 + j * 3 + 2] =
|
||||||
|
data[sy * width * 3 + sx * 3 + 2];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -253,47 +262,57 @@ void PikaCV_Transforms_resize(PikaObj *self, PikaObj* image, int resizeType, int
|
|||||||
arg_deinit(arg_data_new);
|
arg_deinit(arg_data_new);
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef MAX
|
void PikaCV_Transforms_adaptiveThreshold(PikaObj* self,
|
||||||
#undef MIN
|
int c,
|
||||||
|
PikaObj* image,
|
||||||
void PikaCV_Transforms_adaptiveThreshold(PikaObj *self, int c, PikaObj* image, int maxval, int method, int subsize){
|
int maxval,
|
||||||
|
int method,
|
||||||
|
int subsize) {
|
||||||
PikaCV_Image* src = obj_getStruct(image, "image");
|
PikaCV_Image* src = obj_getStruct(image, "image");
|
||||||
|
|
||||||
if (NULL == src) {
|
if (NULL == src) {
|
||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(c<-255||c>255){
|
if (c < 0 || c > 255) {
|
||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
||||||
PikaCV_Converter_toGray(self,image);
|
PikaCV_Converter_toGray(self, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
int size = src->size;
|
int size = src->size;
|
||||||
uint8_t* src_data = _image_getData(image);
|
uint8_t* src_data = _image_getData(image);
|
||||||
|
uint8_t* src_copy;
|
||||||
|
src_copy = (uint8_t*)calloc(size, 1);
|
||||||
|
|
||||||
switch (method)
|
memcpy(src_copy, src_data, size);
|
||||||
{
|
|
||||||
case 0:
|
if (method == 0) {
|
||||||
PikaCV_Filter_meanFilter(self,image,subsize,subsize); //均值滤波
|
PikaCV_Filter_meanFilter(self, image, subsize, subsize); //均值滤波
|
||||||
break;
|
} else if (method == 1) {
|
||||||
case 1:
|
PikaCV_Filter_medianFilter(self, image); //中值滤波
|
||||||
PikaCV_Filter_medianFilter(self,image); //中值滤波
|
} else {
|
||||||
break;
|
free(src_copy);
|
||||||
default:
|
return;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* update size after filter */
|
||||||
|
size = src->size;
|
||||||
uint8_t* smooth_data = _image_getData(image);
|
uint8_t* smooth_data = _image_getData(image);
|
||||||
for(int i=0;i<size;i++){
|
for (int i = 0; i < size; i++) {
|
||||||
smooth_data[i] -= c;
|
uint8_t result = smooth_data[i] - (uint8_t)c;
|
||||||
|
smooth_data[i] =
|
||||||
|
((result < MIN(smooth_data[i], (uint8_t)c)) ? 0 : result);
|
||||||
}
|
}
|
||||||
for(int i=0;i<size;i++){
|
for (int i = 0; i < size; i++) {
|
||||||
src_data[i] = src_data[i] > smooth_data[i] ? maxval : 0 ;
|
src_copy[i] = ((src_copy[i] > smooth_data[i]) ? (uint8_t)maxval : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
obj_setBytes(image, "_data", src_data, src->size);
|
obj_setBytes(image, "_data", src_copy, size);
|
||||||
|
free(src_copy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#undef MAX
|
||||||
|
#undef MIN
|
@ -37,7 +37,11 @@ void PikaCV_Transforms_rotateDown(PikaObj* self, PikaObj* image) {
|
|||||||
arg_deinit(arg_data_new);
|
arg_deinit(arg_data_new);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PikaCV_Transforms_threshold(PikaObj *self, PikaObj* image, int maxval, int thre, int thresholdType){
|
void PikaCV_Transforms_threshold(PikaObj* self,
|
||||||
|
PikaObj* image,
|
||||||
|
int maxval,
|
||||||
|
int thre,
|
||||||
|
int thresholdType) {
|
||||||
PikaCV_Image* src = obj_getStruct(image, "image");
|
PikaCV_Image* src = obj_getStruct(image, "image");
|
||||||
|
|
||||||
if (NULL == src) {
|
if (NULL == src) {
|
||||||
@ -45,40 +49,35 @@ void PikaCV_Transforms_threshold(PikaObj *self, PikaObj* image, int maxval, int
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
||||||
PikaCV_Converter_toGray(self,image);
|
PikaCV_Converter_toGray(self, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t* src_data = _image_getData(image);
|
uint8_t* src_data = _image_getData(image);
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
if(thresholdType == 0 ){
|
if (thresholdType == 0) {
|
||||||
for (i = 0; i < (src->size) ; i++) {
|
for (i = 0; i < (src->size); i++) {
|
||||||
src_data[i] = src_data[i] > thre ? maxval : 0 ;
|
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 == 1 ){
|
} else if (thresholdType == 2) {
|
||||||
for (i = 0; i < (src->size) ; i++) {
|
for (i = 0; i < (src->size); i++) {
|
||||||
src_data[i] = src_data[i] > thre ? 0 : maxval ;
|
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 == 2 ){
|
} else if (thresholdType == 4) {
|
||||||
for (i = 0; i < (src->size) ; i++) {
|
for (i = 0; i < (src->size); i++) {
|
||||||
src_data[i] = src_data[i] > thre ? thre : src_data[i] ;
|
src_data[i] = src_data[i] > thre ? 0 : src_data[i];
|
||||||
}
|
}
|
||||||
}
|
} else if (thresholdType == 5) {
|
||||||
else if(thresholdType == 3 ){
|
PikaCV_Transforms_setOTSU(self, image);
|
||||||
for (i = 0; i < (src->size) ; i++) {
|
} else {
|
||||||
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] ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if(thresholdType == 5 ){
|
|
||||||
PikaCV_Transforms_setOTSU(self,image);
|
|
||||||
}else{
|
|
||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -86,7 +85,12 @@ void PikaCV_Transforms_threshold(PikaObj *self, PikaObj* image, int maxval, int
|
|||||||
obj_setBytes(image, "_data", src_data, src->size);
|
obj_setBytes(image, "_data", src_data, src->size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PikaCV_Transforms_setROI(PikaObj *self, int h, PikaObj* image, int w, int x, int y){
|
void PikaCV_Transforms_setROI(PikaObj* self,
|
||||||
|
int h,
|
||||||
|
PikaObj* image,
|
||||||
|
int w,
|
||||||
|
int x,
|
||||||
|
int y) {
|
||||||
PikaCV_Image* src = obj_getStruct(image, "image");
|
PikaCV_Image* src = obj_getStruct(image, "image");
|
||||||
int width = src->width;
|
int width = src->width;
|
||||||
int height = src->height;
|
int height = src->height;
|
||||||
@ -96,13 +100,13 @@ void PikaCV_Transforms_setROI(PikaObj *self, int h, PikaObj* image, int w, int x
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (src->format != PikaCV_ImageFormat_Type_RGB888) {
|
if (src->format != PikaCV_ImageFormat_Type_RGB888) {
|
||||||
PikaCV_Converter_toRGB888(self,image);
|
PikaCV_Converter_toRGB888(self, image);
|
||||||
}
|
}
|
||||||
if( x <= 0 || y <= 0 || w <= 0 || h <= 0){
|
if (x <= 0 || y <= 0 || w <= 0 || h <= 0) {
|
||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if( x + w > width || y + h > height ){
|
if (x + w > width || y + h > height) {
|
||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -112,11 +116,14 @@ void PikaCV_Transforms_setROI(PikaObj *self, int h, PikaObj* image, int w, int x
|
|||||||
uint8_t* data = _image_getData(image);
|
uint8_t* data = _image_getData(image);
|
||||||
uint8_t* data_new = arg_getBytes(arg_data_new);
|
uint8_t* data_new = arg_getBytes(arg_data_new);
|
||||||
|
|
||||||
for(int i = 0 ; i < h ; i++){
|
for (int i = 0; i < h; i++) {
|
||||||
for(int j = 0 ; j < w ; j++){
|
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] =
|
||||||
data_new[i * 3 * w + j * 3 + 1] = data[ (i + y - 1 ) * width * 3 + ( j + x - 1 ) * 3 + 1];
|
data[(i + y - 1) * width * 3 + (j + x - 1) * 3];
|
||||||
data_new[i * 3 * w + j * 3 + 2] = data[ (i + y - 1 ) * width * 3 + ( j + x - 1 ) * 3 + 2];
|
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->height = h;
|
||||||
@ -125,10 +132,9 @@ void PikaCV_Transforms_setROI(PikaObj *self, int h, PikaObj* image, int w, int x
|
|||||||
|
|
||||||
obj_setBytes(image, "_data", data_new, size_new);
|
obj_setBytes(image, "_data", data_new, size_new);
|
||||||
arg_deinit(arg_data_new);
|
arg_deinit(arg_data_new);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int PikaCV_Transforms_getOTSUthre(PikaObj *self, PikaObj* image){
|
int PikaCV_Transforms_getOTSUthre(PikaObj* self, PikaObj* image) {
|
||||||
PikaCV_Image* src = obj_getStruct(image, "image");
|
PikaCV_Image* src = obj_getStruct(image, "image");
|
||||||
|
|
||||||
if (NULL == src) {
|
if (NULL == src) {
|
||||||
@ -136,40 +142,39 @@ int PikaCV_Transforms_getOTSUthre(PikaObj *self, PikaObj* image){
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
||||||
PikaCV_Converter_toGray(self,image);
|
PikaCV_Converter_toGray(self, image);
|
||||||
}
|
}
|
||||||
const int Grayscale=256;
|
const int Grayscale = 256;
|
||||||
int width = src->width;
|
int width = src->width;
|
||||||
int height = src->height;
|
int height = src->height;
|
||||||
int size = src->size;
|
int size = src->size;
|
||||||
int threshold = 0;
|
int threshold = 0;
|
||||||
int graynum[256] = { 0 };
|
int graynum[256] = {0};
|
||||||
uint8_t* data = _image_getData(image);
|
uint8_t* data = _image_getData(image);
|
||||||
for(int i=0 ; i < size ; i++){
|
for (int i = 0; i < size; i++) {
|
||||||
graynum[data[i]]++;
|
graynum[data[i]]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
double P[256] = { 0.0 };
|
double P[256] = {0.0};
|
||||||
double PK[256] = { 0.0 };
|
double PK[256] = {0.0};
|
||||||
double MK[256] = { 0.0 };
|
double MK[256] = {0.0};
|
||||||
double srcpixnum = width*height, sumtmpPK = 0, sumtmpMK = 0;
|
double srcpixnum = width * height, sumtmpPK = 0, sumtmpMK = 0;
|
||||||
for (int i = 0; i < Grayscale; ++i){
|
for (int i = 0; i < Grayscale; ++i) {
|
||||||
P[i] = graynum[i] / srcpixnum;
|
P[i] = graynum[i] / srcpixnum;
|
||||||
PK[i] = sumtmpPK + P[i];
|
PK[i] = sumtmpPK + P[i];
|
||||||
sumtmpPK = PK[i];
|
sumtmpPK = PK[i];
|
||||||
MK[i] = sumtmpMK + i*P[i];
|
MK[i] = sumtmpMK + i * P[i];
|
||||||
sumtmpMK = MK[i];
|
sumtmpMK = MK[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
double Var=0;
|
double Var = 0;
|
||||||
for (int k = 0; k < Grayscale; ++k){
|
for (int k = 0; k < Grayscale; ++k) {
|
||||||
if(PK[k] > 0.0)
|
if (PK[k] > 0.0) {
|
||||||
{
|
double t = MK[Grayscale - 1] * PK[k] - MK[k];
|
||||||
double t = MK[Grayscale-1] * PK[k] - MK[k];
|
|
||||||
double w = PK[k] * (1 - PK[k]);
|
double w = PK[k] * (1 - PK[k]);
|
||||||
double variance = t * t / w ;
|
double variance = t * t / w;
|
||||||
if (variance > Var){
|
if (variance > Var) {
|
||||||
Var = variance ;
|
Var = variance;
|
||||||
threshold = k;
|
threshold = k;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -178,7 +183,7 @@ int PikaCV_Transforms_getOTSUthre(PikaObj *self, PikaObj* image){
|
|||||||
return threshold;
|
return threshold;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PikaCV_Transforms_setOTSU(PikaObj *self, PikaObj* image){
|
void PikaCV_Transforms_setOTSU(PikaObj* self, PikaObj* image) {
|
||||||
PikaCV_Image* src = obj_getStruct(image, "image");
|
PikaCV_Image* src = obj_getStruct(image, "image");
|
||||||
|
|
||||||
if (NULL == src) {
|
if (NULL == src) {
|
||||||
@ -186,15 +191,15 @@ void PikaCV_Transforms_setOTSU(PikaObj *self, PikaObj* image){
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
||||||
PikaCV_Converter_toGray(self,image);
|
PikaCV_Converter_toGray(self, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
int thre = PikaCV_Transforms_getOTSUthre(self,image);
|
int thre = PikaCV_Transforms_getOTSUthre(self, image);
|
||||||
uint8_t* src_data = _image_getData(image);
|
uint8_t* src_data = _image_getData(image);
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < (src->size) ; i++) {
|
for (i = 0; i < (src->size); i++) {
|
||||||
src_data[i] = src_data[i] > thre ? 255 : 0 ;
|
src_data[i] = src_data[i] > thre ? 255 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
obj_setBytes(image, "_data", src_data, src->size);
|
obj_setBytes(image, "_data", src_data, src->size);
|
||||||
@ -203,7 +208,11 @@ void PikaCV_Transforms_setOTSU(PikaObj *self, PikaObj* image){
|
|||||||
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||||
|
|
||||||
void PikaCV_Transforms_resize(PikaObj *self, PikaObj* image, int resizeType, int x, int y){
|
void PikaCV_Transforms_resize(PikaObj* self,
|
||||||
|
PikaObj* image,
|
||||||
|
int resizeType,
|
||||||
|
int x,
|
||||||
|
int y) {
|
||||||
PikaCV_Image* src = obj_getStruct(image, "image");
|
PikaCV_Image* src = obj_getStruct(image, "image");
|
||||||
int width = src->width;
|
int width = src->width;
|
||||||
int height = src->height;
|
int height = src->height;
|
||||||
@ -211,12 +220,12 @@ void PikaCV_Transforms_resize(PikaObj *self, PikaObj* image, int resizeType, int
|
|||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if( x <= 0 || y <= 0 ){
|
if (x <= 0 || y <= 0) {
|
||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (src->format != PikaCV_ImageFormat_Type_RGB888) {
|
if (src->format != PikaCV_ImageFormat_Type_RGB888) {
|
||||||
PikaCV_Converter_toRGB888(self,image);
|
PikaCV_Converter_toRGB888(self, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
int size_new = x * y * 3;
|
int size_new = x * y * 3;
|
||||||
@ -226,22 +235,22 @@ void PikaCV_Transforms_resize(PikaObj *self, PikaObj* image, int resizeType, int
|
|||||||
|
|
||||||
double scale_x = (double)width / (double)x;
|
double scale_x = (double)width / (double)x;
|
||||||
double scale_y = (double)height / (double)y;
|
double scale_y = (double)height / (double)y;
|
||||||
if(resizeType == 0){
|
if (resizeType == 0) {
|
||||||
for (int i = 0; i < y ; ++i)
|
for (int i = 0; i < y; ++i) {
|
||||||
{
|
|
||||||
int sy = floor(i * scale_y);
|
int sy = floor(i * scale_y);
|
||||||
sy = MIN(sy , ( height - 1 ));
|
sy = MIN(sy, (height - 1));
|
||||||
for (int j = 0; j < x ; ++j)
|
for (int j = 0; j < x; ++j) {
|
||||||
{
|
|
||||||
int sx = floor(j * scale_x);
|
int sx = floor(j * scale_x);
|
||||||
sx = MIN(sx , ( width - 1 ));
|
sx = MIN(sx, (width - 1));
|
||||||
|
|
||||||
data_new[i * x * 3 + j * 3]=data[sy * width * 3 + sx * 3];
|
data_new[i * x * 3 + j * 3] = data[sy * width * 3 + sx * 3];
|
||||||
data_new[i * x * 3 + j * 3 + 1]=data[sy * width * 3 + sx * 3 + 1];
|
data_new[i * x * 3 + j * 3 + 1] =
|
||||||
data_new[i * x * 3 + j * 3 + 2]=data[sy * width * 3 + sx * 3 + 2];
|
data[sy * width * 3 + sx * 3 + 1];
|
||||||
|
data_new[i * x * 3 + j * 3 + 2] =
|
||||||
|
data[sy * width * 3 + sx * 3 + 2];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -253,50 +262,52 @@ void PikaCV_Transforms_resize(PikaObj *self, PikaObj* image, int resizeType, int
|
|||||||
arg_deinit(arg_data_new);
|
arg_deinit(arg_data_new);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PikaCV_Transforms_adaptiveThreshold(PikaObj* self,
|
||||||
|
int c,
|
||||||
void PikaCV_Transforms_adaptiveThreshold(PikaObj *self, int c, PikaObj* image, int maxval, int method, int subsize){
|
PikaObj* image,
|
||||||
|
int maxval,
|
||||||
|
int method,
|
||||||
|
int subsize) {
|
||||||
PikaCV_Image* src = obj_getStruct(image, "image");
|
PikaCV_Image* src = obj_getStruct(image, "image");
|
||||||
|
|
||||||
if (NULL == src) {
|
if (NULL == src) {
|
||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(c < 0 || c > 255){
|
if (c < 0 || c > 255) {
|
||||||
pika_assert(0);
|
pika_assert(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
if (src->format != PikaCV_ImageFormat_Type_GRAY) {
|
||||||
PikaCV_Converter_toGray(self,image);
|
PikaCV_Converter_toGray(self, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
int size = src->size;
|
int size = src->size;
|
||||||
uint8_t* src_data = _image_getData(image);
|
uint8_t* src_data = _image_getData(image);
|
||||||
uint8_t* src_copy ;
|
uint8_t* src_copy;
|
||||||
src_copy = (uint8_t *)calloc(size,1);
|
src_copy = (uint8_t*)calloc(size, 1);
|
||||||
|
|
||||||
memcpy(src_copy,src_data,size);
|
memcpy(src_copy, src_data, size);
|
||||||
|
|
||||||
if (method==0)
|
if (method == 0) {
|
||||||
{
|
PikaCV_Filter_meanFilter(self, image, subsize, subsize); //均值滤波
|
||||||
PikaCV_Filter_meanFilter(self,image,subsize,subsize); //均值滤波
|
} else if (method == 1) {
|
||||||
}
|
PikaCV_Filter_medianFilter(self, image); //中值滤波
|
||||||
else if(method == 1){
|
} else {
|
||||||
PikaCV_Filter_medianFilter(self,image); //中值滤波
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
free(src_copy);
|
free(src_copy);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* update size after filter */
|
||||||
|
size = src->size;
|
||||||
uint8_t* smooth_data = _image_getData(image);
|
uint8_t* smooth_data = _image_getData(image);
|
||||||
for(int i=0;i<size;i++){
|
for (int i = 0; i < size; i++) {
|
||||||
|
|
||||||
uint8_t result = smooth_data[i] - (uint8_t)c;
|
uint8_t result = smooth_data[i] - (uint8_t)c;
|
||||||
smooth_data[i] = ((result < MIN(smooth_data[i], (uint8_t)c)) ? 0 : result);
|
smooth_data[i] =
|
||||||
|
((result < MIN(smooth_data[i], (uint8_t)c)) ? 0 : result);
|
||||||
}
|
}
|
||||||
for(int i=0;i<size;i++){
|
for (int i = 0; i < size; i++) {
|
||||||
src_copy[i] = ((src_copy[i] > smooth_data[i]) ? (uint8_t)maxval : 0) ;
|
src_copy[i] = ((src_copy[i] > smooth_data[i]) ? (uint8_t)maxval : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
obj_setBytes(image, "_data", src_copy, size);
|
obj_setBytes(image, "_data", src_copy, size);
|
||||||
|
@ -136,7 +136,7 @@ TEST(PikaCV, test9) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//! test faild in valgrind.sh, need fix.
|
//! test faild in valgrind.sh, need fix.
|
||||||
#if 0
|
#if 1
|
||||||
TEST(PikaCV, test10) {
|
TEST(PikaCV, test10) {
|
||||||
/* init */
|
/* init */
|
||||||
pikaMemInfo.heapUsedMax = 0;
|
pikaMemInfo.heapUsedMax = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user