mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
Add class filter and meanfilter method in PikaCV
This commit is contained in:
parent
8d89597a1c
commit
ec8611260e
@ -137,4 +137,10 @@ class Transforms(TinyObj):
|
||||
0:NEAREST
|
||||
TODO:
|
||||
1:BILINEAR
|
||||
"""
|
||||
"""
|
||||
|
||||
class Filter(TinyObj):
|
||||
"""The Filter class is used to
|
||||
supply some Image Filtering Algorithms ."""
|
||||
def meanFilter(image:Image,ksizex:int,ksizey:int):
|
||||
""" mean filter,ksize is odd"""
|
114
package/PikaCV/PikaCV_Filter.c
Normal file
114
package/PikaCV/PikaCV_Filter.c
Normal file
@ -0,0 +1,114 @@
|
||||
#include "PikaCV_Filter.h"
|
||||
#include "PikaCV_Converter.h"
|
||||
#include "PikaCV_common.h"
|
||||
|
||||
void PikaCV_Filter_meanFilter(PikaObj *self, PikaObj *image, int ksizex, int ksizey)
|
||||
{
|
||||
PikaCV_Image *src = obj_getStruct(image, "image");
|
||||
int width = src->width;
|
||||
int height = src->height;
|
||||
|
||||
if (NULL == src)
|
||||
{
|
||||
pika_assert(0);
|
||||
return;
|
||||
}
|
||||
if (ksizex % 2 == 0 || ksizey % 2 == 0)
|
||||
{
|
||||
pika_assert(0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (src->format == PikaCV_ImageFormat_Type_GRAY)
|
||||
{
|
||||
int hh = (ksizey - 1) / 2;
|
||||
int hw = (ksizex - 1) / 2;
|
||||
int sum = 0;
|
||||
int mean = 0;
|
||||
int area = ksizex * ksizey;
|
||||
int width_new = width - ksizex + 1;
|
||||
int height_new = height - ksizey + 1;
|
||||
int size_new = width_new * height_new;
|
||||
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 = hh; i < height_new + hh; ++i)
|
||||
{
|
||||
for (int j = hw; j < width_new + hw; ++j)
|
||||
{
|
||||
|
||||
for (int r = i - hh; r <= i + hh; ++r)
|
||||
{
|
||||
for (int c = j - hw; c <= j + hw; ++c)
|
||||
{
|
||||
// sum = Newsrc.at<uchar>(r, c) + sum;
|
||||
sum += data[r * width + c];
|
||||
}
|
||||
}
|
||||
// mean = sum / (wsize.area());
|
||||
// dst.at<uchar>(i-hh,j-hw)=mean;
|
||||
mean = sum / area;
|
||||
data_new[(i - hh) * width_new + (j - hw)] = mean;
|
||||
sum = 0;
|
||||
mean = 0;
|
||||
}
|
||||
}
|
||||
src->height = height_new;
|
||||
src->width = width_new;
|
||||
src->size = size_new;
|
||||
obj_setBytes(image, "_data", data_new, size_new);
|
||||
arg_deinit(arg_data_new);
|
||||
|
||||
return;
|
||||
}
|
||||
else if (src->format != PikaCV_ImageFormat_Type_RGB888)
|
||||
{
|
||||
PikaCV_Converter_toRGB888(self, image);
|
||||
}
|
||||
|
||||
int hh = (ksizey - 1) / 2;
|
||||
int hw = (ksizex - 1) / 2;
|
||||
int sumr = 0, sumg = 0, sumb = 0;
|
||||
int meanr = 0, meang = 0, meanb = 0;
|
||||
int area = ksizex * ksizey;
|
||||
int width_new = width - ksizex + 1;
|
||||
int height_new = height - ksizey + 1;
|
||||
int size_new = width_new * height_new * 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 = hh; i < height_new + hh; ++i)
|
||||
{
|
||||
for (int j = hw; j < width_new + hw; ++j)
|
||||
{
|
||||
for (int r = i - hh; r <= i + hh; ++r)
|
||||
{
|
||||
for (int c = j - hw; c <= j + hw; ++c)
|
||||
{
|
||||
sumr += data[r * width * 3 + c * 3];
|
||||
sumg += data[r * width * 3 + c * 3 + 1];
|
||||
sumb += data[r * width * 3 + c * 3 + 2];
|
||||
}
|
||||
}
|
||||
meanr = sumr / area;
|
||||
meang = sumg / area;
|
||||
meanb = sumb / area;
|
||||
data_new[(i - hh) * width_new * 3 + (j - hw) * 3] = meanr;
|
||||
data_new[(i - hh) * width_new * 3 + (j - hw) * 3 + 1] = meang;
|
||||
data_new[(i - hh) * width_new * 3 + (j - hw) * 3 + 2] = meanb;
|
||||
sumr = 0;
|
||||
sumg = 0;
|
||||
sumb = 0;
|
||||
meanb = 0;
|
||||
meang = 0;
|
||||
meanr = 0;
|
||||
}
|
||||
}
|
||||
src->height = height_new;
|
||||
src->width = width_new;
|
||||
src->size = size_new;
|
||||
obj_setBytes(image, "_data", data_new, size_new);
|
||||
arg_deinit(arg_data_new);
|
||||
|
||||
return;
|
||||
}
|
@ -137,4 +137,10 @@ class Transforms(TinyObj):
|
||||
0:NEAREST
|
||||
TODO:
|
||||
1:BILINEAR
|
||||
"""
|
||||
"""
|
||||
|
||||
class Filter(TinyObj):
|
||||
"""The Filter class is used to
|
||||
supply some Image Filtering Algorithms ."""
|
||||
def meanFilter(image:Image,ksizex:int,ksizey:int):
|
||||
""" mean filter,ksize is odd"""
|
@ -0,0 +1,114 @@
|
||||
#include "PikaCV_Filter.h"
|
||||
#include "PikaCV_Converter.h"
|
||||
#include "PikaCV_common.h"
|
||||
|
||||
void PikaCV_Filter_meanFilter(PikaObj *self, PikaObj *image, int ksizex, int ksizey)
|
||||
{
|
||||
PikaCV_Image *src = obj_getStruct(image, "image");
|
||||
int width = src->width;
|
||||
int height = src->height;
|
||||
|
||||
if (NULL == src)
|
||||
{
|
||||
pika_assert(0);
|
||||
return;
|
||||
}
|
||||
if (ksizex % 2 == 0 || ksizey % 2 == 0)
|
||||
{
|
||||
pika_assert(0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (src->format == PikaCV_ImageFormat_Type_GRAY)
|
||||
{
|
||||
int hh = (ksizey - 1) / 2;
|
||||
int hw = (ksizex - 1) / 2;
|
||||
int sum = 0;
|
||||
int mean = 0;
|
||||
int area = ksizex * ksizey;
|
||||
int width_new = width - ksizex + 1;
|
||||
int height_new = height - ksizey + 1;
|
||||
int size_new = width_new * height_new;
|
||||
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 = hh; i < height_new + hh; ++i)
|
||||
{
|
||||
for (int j = hw; j < width_new + hw; ++j)
|
||||
{
|
||||
|
||||
for (int r = i - hh; r <= i + hh; ++r)
|
||||
{
|
||||
for (int c = j - hw; c <= j + hw; ++c)
|
||||
{
|
||||
// sum = Newsrc.at<uchar>(r, c) + sum;
|
||||
sum += data[r * width + c];
|
||||
}
|
||||
}
|
||||
// mean = sum / (wsize.area());
|
||||
// dst.at<uchar>(i-hh,j-hw)=mean;
|
||||
mean = sum / area;
|
||||
data_new[(i - hh) * width_new + (j - hw)] = mean;
|
||||
sum = 0;
|
||||
mean = 0;
|
||||
}
|
||||
}
|
||||
src->height = height_new;
|
||||
src->width = width_new;
|
||||
src->size = size_new;
|
||||
obj_setBytes(image, "_data", data_new, size_new);
|
||||
arg_deinit(arg_data_new);
|
||||
|
||||
return;
|
||||
}
|
||||
else if (src->format != PikaCV_ImageFormat_Type_RGB888)
|
||||
{
|
||||
PikaCV_Converter_toRGB888(self, image);
|
||||
}
|
||||
|
||||
int hh = (ksizey - 1) / 2;
|
||||
int hw = (ksizex - 1) / 2;
|
||||
int sumr = 0, sumg = 0, sumb = 0;
|
||||
int meanr = 0, meang = 0, meanb = 0;
|
||||
int area = ksizex * ksizey;
|
||||
int width_new = width - ksizex + 1;
|
||||
int height_new = height - ksizey + 1;
|
||||
int size_new = width_new * height_new * 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 = hh; i < height_new + hh; ++i)
|
||||
{
|
||||
for (int j = hw; j < width_new + hw; ++j)
|
||||
{
|
||||
for (int r = i - hh; r <= i + hh; ++r)
|
||||
{
|
||||
for (int c = j - hw; c <= j + hw; ++c)
|
||||
{
|
||||
sumr += data[r * width * 3 + c * 3];
|
||||
sumg += data[r * width * 3 + c * 3 + 1];
|
||||
sumb += data[r * width * 3 + c * 3 + 2];
|
||||
}
|
||||
}
|
||||
meanr = sumr / area;
|
||||
meang = sumg / area;
|
||||
meanb = sumb / area;
|
||||
data_new[(i - hh) * width_new * 3 + (j - hw) * 3] = meanr;
|
||||
data_new[(i - hh) * width_new * 3 + (j - hw) * 3 + 1] = meang;
|
||||
data_new[(i - hh) * width_new * 3 + (j - hw) * 3 + 2] = meanb;
|
||||
sumr = 0;
|
||||
sumg = 0;
|
||||
sumb = 0;
|
||||
meanb = 0;
|
||||
meang = 0;
|
||||
meanr = 0;
|
||||
}
|
||||
}
|
||||
src->height = height_new;
|
||||
src->width = width_new;
|
||||
src->size = size_new;
|
||||
obj_setBytes(image, "_data", data_new, size_new);
|
||||
arg_deinit(arg_data_new);
|
||||
|
||||
return;
|
||||
}
|
@ -102,5 +102,20 @@ TEST(PikaCV, test7) {
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(PikaCV, test8) {
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
/* run */
|
||||
__platform_printf("BEGIN\r\n");
|
||||
pikaVM_runSingleFile(pikaMain, "test/python/PikaCV/PikaCV_test8.py");
|
||||
/* collect */
|
||||
/* assert */
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
@ -2,6 +2,6 @@ import PikaCV as cv
|
||||
img = cv.Image()
|
||||
img.read("test/assets/test.jpg")
|
||||
cv.Converter.toRGB888(img)
|
||||
thre = cv.Transforms.resize(img,25,25)
|
||||
thre = cv.Transforms.resize(img,25,25,0)
|
||||
cv.Converter.toBMP(img)
|
||||
img.write("test/out/test3.bmp")
|
7
port/linux/test/python/PikaCV/PikaCV_test8.py
Normal file
7
port/linux/test/python/PikaCV/PikaCV_test8.py
Normal file
@ -0,0 +1,7 @@
|
||||
import PikaCV as cv
|
||||
img = cv.Image()
|
||||
img.read("test/assets/test.jpg")
|
||||
cv.Converter.toRGB888(img)
|
||||
thre = cv.Filter.meanFilter(img,3,3)
|
||||
cv.Converter.toBMP(img)
|
||||
img.write("test/out/test4.bmp")
|
Loading…
x
Reference in New Issue
Block a user