split, merge test ok

This commit is contained in:
pikastech 2022-07-15 15:14:57 +08:00 committed by 李昂
parent 000ee776b3
commit 126b661af3
9 changed files with 195 additions and 51 deletions

View File

@ -79,6 +79,12 @@ class Image(TinyObj):
def minus(self,image:Imgae):
"""Minus two images"""
def split(self) -> List:
"""Split one 3-channels image to three 1-channel"""
def merge(self,R:Image,G:Image,B:Image):
"""Merge three 1-channel image to 3-channels"""
class Converter(TinyObj):
"""The Converter class is used to
convert an image from one format to another."""

View File

@ -286,7 +286,7 @@ void PikaCV_Converter_toRGB888(PikaObj* self, PikaObj* image) {
}
exit:
obj_setBytes(image, "_data", data_new, size_new);
img->format = PikaCV_ImageFormat_Type_RGB565;
img->format = PikaCV_ImageFormat_Type_RGB888;
img->size = size_new;
arg_deinit(arg_data_new);
}

View File

@ -1,5 +1,6 @@
#include "PikaCV_Image.h"
#include "PikaCV_common.h"
#include "PikaStdData_List.h"
#include "dataStrs.h"
void PikaCV_Image___init__(PikaObj* self) {
@ -221,27 +222,27 @@ void PikaCV_Image_write(PikaObj* self, char* path) {
__platform_fclose(fp);
}
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
void PikaCV_Image_add(PikaObj *self, PikaObj* image){
void PikaCV_Image_add(PikaObj* self, PikaObj* image) {
PikaCV_Image* src = obj_getStruct(self, "image");
PikaCV_Image* img = obj_getStruct(image, "image");
if (NULL == src || NULL == img) {
pika_assert(0);
return;
}
if (img->format != src->format ) {
if (img->format != src->format) {
obj_setErrorCode(self, PIKA_RES_ERR_OPERATION_FAILED);
__platform_printf("unsupported image format\n");
return;
}
if (img->size != src->size ) {
if (img->size != src->size) {
obj_setErrorCode(self, PIKA_RES_ERR_OPERATION_FAILED);
__platform_printf("illegal image size\n");
return;
}
uint8_t* src_data = _image_getData(self);
uint8_t* img_data = _image_getData(image);
@ -249,40 +250,43 @@ void PikaCV_Image_add(PikaObj *self, PikaObj* image){
uint8_t result;
/* add two images */
for (i = 0; i < (src->size)/3; i++) {
result=src_data[i * 3] + img_data[i * 3];
src_data[i * 3] = ((result<MAX(src_data[i * 3],img_data[i * 3]))?255:result);
result=src_data[i * 3 + 1] + img_data[i * 3 + 1];
src_data[i * 3 + 1] = ((result<MAX(src_data[i * 3 + 1],img_data[i * 3 + 1]))?255:result);
for (i = 0; i < (src->size) / 3; i++) {
result = src_data[i * 3] + img_data[i * 3];
src_data[i * 3] =
((result < MAX(src_data[i * 3], img_data[i * 3])) ? 255 : result);
result=src_data[i * 3 + 2] + img_data[i * 3 + 2];
src_data[i * 3 + 2] = ((result<MAX(src_data[i * 3 + 2],img_data[i * 3 + 2]))?255:result);
result = src_data[i * 3 + 1] + img_data[i * 3 + 1];
src_data[i * 3 + 1] =
((result < MAX(src_data[i * 3 + 1], img_data[i * 3 + 1])) ? 255
: result);
result = src_data[i * 3 + 2] + img_data[i * 3 + 2];
src_data[i * 3 + 2] =
((result < MAX(src_data[i * 3 + 2], img_data[i * 3 + 2])) ? 255
: result);
}
obj_setBytes(self, "_data", src_data, src->size);
}
void PikaCV_Image_minus(PikaObj *self, PikaObj* image){
void PikaCV_Image_minus(PikaObj* self, PikaObj* image) {
PikaCV_Image* src = obj_getStruct(self, "image");
PikaCV_Image* img = obj_getStruct(image, "image");
if (NULL == src || NULL == img) {
pika_assert(0);
return;
}
if (img->format != src->format ) {
if (img->format != src->format) {
obj_setErrorCode(self, PIKA_RES_ERR_OPERATION_FAILED);
__platform_printf("unsupported image format\n");
return;
}
if (img->size != src->size ) {
if (img->size != src->size) {
obj_setErrorCode(self, PIKA_RES_ERR_OPERATION_FAILED);
__platform_printf("unsupported image format\n");
__platform_printf("illegal image size\n");
return;
}
uint8_t* src_data = _image_getData(self);
uint8_t* img_data = _image_getData(image);
@ -290,18 +294,123 @@ void PikaCV_Image_minus(PikaObj *self, PikaObj* image){
uint8_t result;
/* minus two images */
for (i = 0; i < (src->size)/3; i++) {
result=src_data[i * 3] - img_data[i * 3];
src_data[i * 3] = ((result<MIN(src_data[i * 3],img_data[i * 3]))?0:result);
result=src_data[i * 3 + 1] - img_data[i * 3 + 1];
src_data[i * 3 + 1] = ((result>MIN(src_data[i * 3 + 1],img_data[i * 3 + 1]))?0:result);
for (i = 0; i < (src->size) / 3; i++) {
result = src_data[i * 3] - img_data[i * 3];
src_data[i * 3] =
((result < MIN(src_data[i * 3], img_data[i * 3])) ? 0 : result);
result=src_data[i * 3 + 2] - img_data[i * 3 + 2];
src_data[i * 3 + 2] = ((result<MIN(src_data[i * 3 + 2],img_data[i * 3 + 2]))?0:result);
result = src_data[i * 3 + 1] - img_data[i * 3 + 1];
src_data[i * 3 + 1] =
((result > MIN(src_data[i * 3 + 1], img_data[i * 3 + 1])) ? 0
: result);
result = src_data[i * 3 + 2] - img_data[i * 3 + 2];
src_data[i * 3 + 2] =
((result < MIN(src_data[i * 3 + 2], img_data[i * 3 + 2])) ? 0
: result);
}
obj_setBytes(self, "_data", src_data, (src->size));
obj_setBytes(self, "_data", src_data, (src->size));
}
void PikaCV_Image_merge(PikaObj* self, PikaObj* B, PikaObj* G, PikaObj* R) {
PikaCV_Image* src = obj_getStruct(self, "image");
PikaCV_Image* Channel_B = obj_getStruct(B, "image");
PikaCV_Image* Channel_G = obj_getStruct(G, "image");
PikaCV_Image* Channel_R = obj_getStruct(R, "image");
if (NULL == src || NULL == Channel_B || NULL == Channel_G ||
NULL == Channel_R) {
pika_assert(0);
return;
}
if (PikaCV_ImageFormat_Type_RGB888 != src->format) {
obj_setErrorCode(self, PIKA_RES_ERR_OPERATION_FAILED);
__platform_printf("unsupported image format\n");
return;
}
if (PikaCV_ImageFormat_Type_GRAY != Channel_B->format ||
PikaCV_ImageFormat_Type_GRAY != Channel_G->format ||
PikaCV_ImageFormat_Type_GRAY != Channel_R->format) {
obj_setErrorCode(self, PIKA_RES_ERR_OPERATION_FAILED);
__platform_printf("unsupported image format\n");
return;
}
if (Channel_B->size != Channel_G->size ||
Channel_B->size != Channel_R->size) {
obj_setErrorCode(self, PIKA_RES_ERR_OPERATION_FAILED);
__platform_printf("illegal image size\n");
return;
}
src->size = Channel_B->size * 3;
src->height = Channel_B->height;
src->width = Channel_B->width;
Arg* src_new_data_arg = arg_setBytes(NULL, "", NULL, src->size);
uint8_t* src_data_new = arg_getBytes(src_new_data_arg);
uint8_t* B_data = _image_getData(B);
uint8_t* G_data = _image_getData(G);
uint8_t* R_data = _image_getData(R);
for (int i = 0; i < Channel_B->size; i++) {
src_data_new[i * 3] = R_data[i];
src_data_new[i * 3 + 1] = G_data[i];
src_data_new[i * 3 + 2] = B_data[i];
}
obj_setBytes(self, "_data", src_data_new, (src->size));
arg_deinit(src_new_data_arg);
}
PikaObj* PikaCV_Image_split(PikaObj* self) {
PikaCV_Image* src = obj_getStruct(self, "image");
if (NULL == src) {
pika_assert(0);
return NULL;
}
if (PikaCV_ImageFormat_Type_RGB888 != src->format) {
obj_setErrorCode(self, PIKA_RES_ERR_OPERATION_FAILED);
__platform_printf("unsupported image format\n");
return NULL;
}
uint8_t* src_data = _image_getData(self);
Arg* arg_data_B = arg_setBytes(NULL, "", NULL, (src->size) / 3);
Arg* arg_data_G = arg_setBytes(NULL, "", NULL, (src->size) / 3);
Arg* arg_data_R = arg_setBytes(NULL, "", NULL, (src->size) / 3);
uint8_t* data_B = arg_getBytes(arg_data_B);
uint8_t* data_G = arg_getBytes(arg_data_G);
uint8_t* data_R = arg_getBytes(arg_data_R);
uint8_t* RGB[3] = {data_R, data_G, data_B};
for (int i = 0; i < (src->size) / 3; i++) {
RGB[0][i] = src_data[i * 3];
RGB[1][i] = src_data[i * 3 + 1];
RGB[2][i] = src_data[i * 3 + 2];
}
PikaObj* list = newNormalObj(New_PikaStdData_List);
PikaStdData_List___init__(list);
for (int i = 0; i < 3; i++) {
PikaObj* img = newNormalObj(New_PikaCV_Image);
PikaCV_Image___init__(img);
PikaCV_Image_loadGray(img, RGB[i], src->width, src->height);
Arg* token_arg = arg_setPtr(NULL, "", ARG_TYPE_OBJECT, img);
/* 添加到 list 对象 */
PikaStdData_List_append(list, token_arg);
/* 销毁 arg */
arg_deinit(token_arg);
}
arg_deinit(arg_data_B);
arg_deinit(arg_data_G);
arg_deinit(arg_data_R);
return list;
}

View File

@ -286,7 +286,7 @@ void PikaCV_Converter_toRGB888(PikaObj* self, PikaObj* image) {
}
exit:
obj_setBytes(image, "_data", data_new, size_new);
img->format = PikaCV_ImageFormat_Type_RGB565;
img->format = PikaCV_ImageFormat_Type_RGB888;
img->size = size_new;
arg_deinit(arg_data_new);
}

View File

@ -343,21 +343,25 @@ void PikaCV_Image_merge(PikaObj* self, PikaObj* B, PikaObj* G, PikaObj* R) {
return;
}
uint8_t* src_data = _image_getData(self);
uint8_t* B_data = _image_getData(B);
uint8_t* G_data = _image_getData(G);
uint8_t* R_data = _image_getData(R);
src->size = Channel_B->size * 3;
src->height = Channel_B->height;
src->width = Channel_B->width;
for (int i = 0; i < src->size; i++) {
src_data[i * 3] = R_data[i];
src_data[i * 3 + 1] = G_data[i];
src_data[i * 3 + 2] = B_data[i];
Arg* src_new_data_arg = arg_setBytes(NULL, "", NULL, src->size);
uint8_t* src_data_new = arg_getBytes(src_new_data_arg);
uint8_t* B_data = _image_getData(B);
uint8_t* G_data = _image_getData(G);
uint8_t* R_data = _image_getData(R);
for (int i = 0; i < Channel_B->size; i++) {
src_data_new[i * 3] = R_data[i];
src_data_new[i * 3 + 1] = G_data[i];
src_data_new[i * 3 + 2] = B_data[i];
}
obj_setBytes(self, "_data", src_data, (src->size));
obj_setBytes(self, "_data", src_data_new, (src->size));
arg_deinit(src_new_data_arg);
}
PikaObj* PikaCV_Image_split(PikaObj* self) {

View File

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

View File

@ -6,5 +6,5 @@ img.read("test/assets/test.jpg")
cv.Converter.toRGB565(img)
print(str(binascii.b2a_hex(img.data())))
cv.Converter.toBMP(img)
img.write("test/assets/test.bmp")
img.write("test/out/test.bmp")

View File

@ -14,12 +14,4 @@ cv.Converter.toBMP(G)
cv.Converter.toBMP(B)
R.write("test/out/R.bmp")
G.write("test/out/G.bmp")
B.write("test/out/B.bmp")
# img1 = cv.Image()
# cv.Converter.toRGB888(img1)
# img1.merge(R,G,B)
# cv.Converter.toBMP(img1)
# img1.write("test/assets/merge.bmp")
# print("Merge done")
B.write("test/out/B.bmp")

View File

@ -0,0 +1,18 @@
import PikaCV as cv
import binascii
img = cv.Image()
img.read("test/assets/test.jpg")
cv.Converter.toRGB888(img)
Channel = img.split()
R = Channel[0]
G = Channel[1]
B = Channel[2]
img1 = cv.Image()
cv.Converter.toRGB888(img1)
img1.merge(R,G,B)
cv.Converter.toBMP(img1)
img1.write("test/out/merge.bmp")
print("Merge done")