mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Add ppm support
This commit is contained in:
parent
5998aa4857
commit
c844da7bec
@ -406,3 +406,14 @@ void imlib_histeq(struct image *src)
|
||||
src->pixels[i] = (uint8_t) ((MAX_GRAY_LEVEL/(float)a) * hist[src->pixels[i]]);
|
||||
}
|
||||
}
|
||||
|
||||
/* those just call ppm for now */
|
||||
int imlib_load_image(image_t *image, const char *path)
|
||||
{
|
||||
return ppm_read(image, path);
|
||||
}
|
||||
|
||||
int imlib_save_image(image_t *image, const char *path, rectangle_t *subimage)
|
||||
{
|
||||
return ppm_write(image, path);
|
||||
}
|
||||
|
||||
@ -191,8 +191,6 @@ void imlib_integral_image_sq(struct image *src, struct integral_image *sum);
|
||||
uint32_t imlib_integral_lookup(struct integral_image *src, int x, int y, int w, int h);
|
||||
|
||||
/* Template matching */
|
||||
int imlib_save_template(struct image *image, const char *path);
|
||||
int imlib_load_template(struct image *image, const char *path);
|
||||
float imlib_template_match(struct image *image, struct image *template, struct rectangle *r);
|
||||
|
||||
/* Haar/VJ */
|
||||
@ -211,4 +209,10 @@ void imlib_draw_circle(struct image *image, int cx, int cy, int r);
|
||||
int imlib_image_mean(struct image *src);
|
||||
void imlib_subimage(struct image *src_img, struct image *dst_img, int x_off, int y_off);
|
||||
void imlib_blit(struct image *dst_img, struct image *src_img, int x_off, int y_off);
|
||||
|
||||
/* Image file functions */
|
||||
int ppm_read(image_t *img, const char *path);
|
||||
int ppm_write(image_t *img, const char *path);
|
||||
int imlib_load_image(image_t *image, const char *path);
|
||||
int imlib_save_image(image_t *image, const char *path, rectangle_t *subimage);
|
||||
#endif //__IMLIB_H__
|
||||
|
||||
120
src/img/ppm.c
Normal file
120
src/img/ppm.c
Normal file
@ -0,0 +1,120 @@
|
||||
#include <libmp.h>
|
||||
#include "xalloc.h"
|
||||
#include "imlib.h"
|
||||
#include "mdefs.h"
|
||||
static int isspace(int c)
|
||||
{
|
||||
return (c=='\n'||c=='\r'||c=='\t'||c==' ');
|
||||
}
|
||||
|
||||
static int f_getc(FIL *fp)
|
||||
{
|
||||
int c;
|
||||
UINT bytes;
|
||||
FRESULT res = f_read(fp, &c, 1, &bytes);
|
||||
if (res != FR_OK || bytes != 1) {
|
||||
c = EOF;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
static DISABLE_OPT int read_num(FIL *fp)
|
||||
{
|
||||
int x=0, c=0;
|
||||
while (1) {
|
||||
c = f_getc(fp);
|
||||
if (c == '#') {
|
||||
/* skip comments */
|
||||
while ((c = f_getc(fp)) != EOF && c != '\n');
|
||||
} else if (c >= 48 && c <= 57) {
|
||||
x *= 10;
|
||||
x += (c-48);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int ppm_write(image_t *img, const char *path)
|
||||
{
|
||||
FIL fp;
|
||||
UINT bytes;
|
||||
FRESULT res=FR_OK;
|
||||
|
||||
res = f_open(&fp, path, FA_WRITE|FA_CREATE_ALWAYS);
|
||||
if (res != FR_OK) {
|
||||
return res;
|
||||
}
|
||||
|
||||
if (img->bpp == 1) {
|
||||
bytes = f_printf(&fp, "P5\n%d %d\n255\n", img->w, img->h);
|
||||
} else {
|
||||
bytes = f_printf(&fp, "P6\n%d %d\n255\n", img->w, img->h);
|
||||
}
|
||||
|
||||
if (bytes == -1) {
|
||||
res = FR_DENIED;
|
||||
goto error;
|
||||
}
|
||||
|
||||
res = f_write(&fp, img->data, img->w*img->h*img->bpp, &bytes);
|
||||
if (res != FR_OK || bytes < (img->w*img->h*img->bpp)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
error:
|
||||
f_close(&fp);
|
||||
return res;
|
||||
}
|
||||
|
||||
int ppm_read(image_t *img, const char *path)
|
||||
{
|
||||
FIL fp;
|
||||
UINT bytes;
|
||||
FRESULT res=FR_OK;
|
||||
|
||||
res = f_open(&fp, path, FA_READ|FA_OPEN_EXISTING);
|
||||
if (res != FR_OK) {
|
||||
return res;
|
||||
}
|
||||
|
||||
int type;
|
||||
/* read image header */
|
||||
if (f_getc(&fp) != 'P' ||
|
||||
((type = f_getc(&fp)) != '5' && type != '6')
|
||||
|| !isspace(f_getc(&fp))) {
|
||||
printf("ppm:image format not supported\n");
|
||||
res = -1;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((img->w = read_num(&fp)) == -1 || /* read image width */
|
||||
(img->h = read_num(&fp)) == -1 || /* read image height */
|
||||
read_num(&fp) != 255) { /* read image max gray */
|
||||
printf("ppm:image format not supported\n");
|
||||
res = -1;
|
||||
goto error;
|
||||
}
|
||||
|
||||
img->bpp = (type=='5')? 1:2;
|
||||
int size = img->w*img->h*img->bpp;
|
||||
|
||||
/* alloc image */
|
||||
img->data = xalloc(size);
|
||||
if (img->data == NULL) {
|
||||
printf("ppm: out of memory\n");
|
||||
res = -1;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* read image data */
|
||||
res = f_read(&fp, img->data, size, &bytes);
|
||||
if (res != FR_OK || bytes != size) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
error:
|
||||
f_close(&fp);
|
||||
return res;
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
import sensor, imlib, time
|
||||
import sensor, time
|
||||
sensor.set_pixformat(sensor.RGB565)
|
||||
clock = time.clock()
|
||||
while (True):
|
||||
@ -7,14 +7,13 @@ while (True):
|
||||
image = sensor.snapshot()
|
||||
|
||||
# detect blobs
|
||||
imlib.threshold(image, (255, 127, 127), 40)
|
||||
imlib.median(image, 3)
|
||||
blobs = imlib.count_blobs(image)
|
||||
image.threshold((255, 127, 127), 40)
|
||||
image.median(3)
|
||||
blobs = image.count_blobs()
|
||||
|
||||
# draw rectangles around detected blobs
|
||||
image = sensor.snapshot()
|
||||
for r in blobs:
|
||||
imlib.draw_rectangle(image, r)
|
||||
image.draw_rectangle(r)
|
||||
|
||||
print(clock.fps())
|
||||
break
|
||||
|
||||
Loading…
Reference in New Issue
Block a user