linux camera 소스에서 질문~!!!!!
글쓴이: aninly / 작성시간: 수, 2005/09/07 - 12:32오후
int main(int argc, char *argv[])
{
.....
if((cam_fp=cam_init())<0) goto err; // /dev/misc/preview를 open한다.
if(ioctl(cam_fp, CMD_CAMER_INIT, &camif_cfg)){ // Camera Driver를 초기화 한다.
....
/* Start Camera Codec */
write(cam_fp,"O",2); //Preview Block & DMA를 Start 시킨다.
while (!found) { //found에 값이 올때 까지 프레임과 골이 같아 질때 까지
frames++; //프레임을 하나씩 늘려라
save_yuv(cam_width, cam_height, bpp, frames); //cam_fp에서 YUV DATA를 읽어서 file로 저장한다.
if( frames == goal ) { found = 1; break;}
}
-
cam_fp에서 YUV DATA를 읽어서 file로 저장하기 전에 YUV DATA를 불러(읽어)들려서 영상필터처리를 할려고 하는데 어떻게 해야될지 잘 모르겠더라고요.
제가 Save함수에서 yuv_fp랑 g_yuv를 이용해서 할려고 했는데 안되러라고요. 변수를 잘 못 지정했는지......
아래의 필터를 거친후에 저장하고 싶은데 안돼요. 2주째 이것에 매달려 있는데, 넘 힘들어요. 학기도 시작되었고...........
제가 삽입할려는 영상필터
//영상필터
unsigned char orgimg2[240][240];
unsigned char orgimg[720][240];
/*필요한 영역 추출해서 orgimg2로 넘김 */
for(k=110 ; k<170 ; k++)
{
for(j=90 ; j<158; j++)
{
int r=j-90, w=k-110 ;
orgimg2[r][w]=orgimg[j][k];
/*orgimg2 이진화시키기*/
if(orgimg2[r][w]>=63)
{
orgimg2[r][w]=255;
}
else orgimg2[r][w]=0;
}
}
static inline void save_yuv(int width, int height, int bpp, int iter)
{
FILE *yuv_fp = NULL;
char file_name[100];
/* read from device */
if (read(cam_fp, g_yuv, width*height*bpp/8) < 0) {
perror("read()");
}
if (bpp == 16 ) {
sprintf(&file_name[0], "422X%d.yuv", iter);
printf("422X%d.yuv", iter);
}
else {
sprintf(&file_name[0], "420X%d.yuv", iter);
printf("420X%d.yuv\n", iter);
}
fflush(stdout);
/* file create/open, note to "wb" */
yuv_fp = fopen(&file_name[0], "wb"); //파일을 오픈해서 yuv_fp로 넣어 준다//file 생성//
if (!yuv_fp) {
perror(&file_name[0]);
}
fwrite(g_yuv, 1, width * height * bpp / 8, yuv_fp); //파일 정보(g_yuv)를 yuv_fp에다가 쓴다//
fclose(yuv_fp);
}
-
여기서bpp=16이 아니면 file_name[0]에는 영상배열이 없고, 422X%d.yuv만 들어가는거죠. %d에는 iter값이 들어가고,
yuv_fp = fopen(&file_name[0], "wb");
fwrite(g_yuv, 1, width * height * bpp / 8, yuv_fp);
는 file_name[0]을 열어서 g_yuv를 저장하는것 아닌가요?
그러면 g_yuv에 영상이 들어 있는거 맞나요?
아래는 소스 전문(全文).
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/ioctl.h>
/* maximum */
#define CAM_WIDTH 720 //카메라 가로의 크기
#define CAM_HEIGHT 240 //카메라 세로의 크기
static int cam_fp = -1;
#include "userapp.h" //camif_param_t 불러오기//
camif_param_t camif_cfg;
static unsigned char *g_yuv; //포인터 전역변수 *g_yuv
#define CODEC_NAME "/dev/misc/codec"
/*****************메모리 할당*********************/
/* i가 size/4될때 까지 g_yuv를 증가시킴 */
/* size=cam_width*cam_height*bpp/8 */
void fmalloc(int size) //메모리할당 함수//
{
unsigned int i=0;
unsigned int *ptr32;
g_yuv = (unsigned char*)malloc(size); //size 메모리할당의 주소
if(!g_yuv)
{
printf("Memory Allcation Failed \n");
exit(1);
}
ptr32 = (unsigned int *)g_yuv; //size메모리 할당의 주소
for (;i<size/4; i++)
*ptr32++ = 0xff00ff00; /* Red */ // i가 size/4될때 까지 ptr32가 가르키는 곳의 값을 증가
} // g_yuv를 증가시킴
/******CODEC_NAME 열어서 열린 값을 dev_fp에 저장후 리턴**********/
static inline int cam_init(void)
{
int dev_fp = -1;
dev_fp = open(CODEC_NAME, O_RDWR); //CODEC_NAME 열어서 dev_fp에 저장
if (dev_fp < 0) //CODEC_NAME이 열리는지 확인
{
perror(CODEC_NAME);
printf("Open Failed \n");
return -1;
}
return dev_fp;
}
/*** width=가로 크기 , height= 세로크기 , bpp=비트 퍼 픽셀 16or12 , iter=파일 번호****/
/***cam_fp와 g_yuv를 읽어서 bpp에 맞게 저장 하는 함수 file_name[] 으로 yuv_fp를 넣는다***/
static inline void save_yuv(int width, int height, int bpp, int iter)
{
FILE *yuv_fp = NULL;
char file_name[100];
/* read from device */
if (read(cam_fp, g_yuv, width*height*bpp/8) < 0) {
perror("read()");
}
if (bpp == 16 ) {
sprintf(&file_name[0], "422X%d.yuv", iter);
printf("422X%d.yuv", iter);
}
else {
sprintf(&file_name[0], "420X%d.yuv", iter);
printf("420X%d.yuv\n", iter);
}
fflush(stdout);
/* file create/open, note to "wb" */
yuv_fp = fopen(&file_name[0], "wb"); //파일을 오픈해서 yuv_fp로 넣어 준다//file 생성//
if (!yuv_fp) {
perror(&file_name[0]);
}
fwrite(g_yuv, 1, width * height * bpp / 8, yuv_fp); //파일 정보(g_yuv)를 yuv_fp에다가 쓴다//
fclose(yuv_fp);
}
/*******************************/
/* 시간당 프레임 시간 계산 함수 */
static inline void print_fps(struct timeval *s, struct timeval *e)
{
unsigned long time;
unsigned long sec;
unsigned long usec;
int fps = 0;
sec = e->tv_sec - s->tv_sec;
if (e->tv_usec > s->tv_usec)
usec = e->tv_usec - s->tv_usec;
else {
usec = e->tv_usec + 1000000 - s->tv_usec;
sec--;
}
time = sec * 1000 + (usec+1) / 1000;
fps = 1000 / (time / 30);
printf("%d fps\n", fps);
}
/****Main 함수************/
int main(int argc, char *argv[])
{
char cmdLine[100];
struct timeval start_tv, end_tv;
struct timezone tz;
int cam_width = CAM_WIDTH;
int cam_height = CAM_HEIGHT;
int i, goal, bpp,frames =0;
int found = 0;
if (argc != 6) {
printf("%s <width> <height> <bpp> <num> <flip>\n", argv[0]);
printf(" width: < 240 \n");
printf(" height: < 320 \n");
printf(" bpp: 420, 422 \n");
printf(" num:\n");
printf(" 0 is non-stop capturing\n"
" X is the capture count\n");
printf(" flip: \n"
" 0 is normal \n"
" 1 is x-axis mirrorX \n"
" 2 is y-axis mirrorX \n"
" 3 is 180 rotation \n");
printf("EX) \n"
" T_SHELL> %s 240 320 420 0 0 \n", argv[0]);
goto err;
}
camif_cfg.dst_x = cam_width = atoi(argv[1]);
camif_cfg.dst_y = cam_height = atoi(argv[2]);
bpp = atoi(argv[3]);
switch (bpp) {
case 422:
camif_cfg.bpp = 422;
bpp = 16;
break;
case 420:
camif_cfg.bpp = 420;
bpp = 12;
break;
default:
printf("Wrong Bpp format 422:420 \n");
return;
}
fmalloc(cam_width*cam_height*bpp/8);
goal = atoi(argv[4]);
camif_cfg.flip = atoi(argv[5]);
if ((cam_fp = cam_init()) < 0) //CODEC_NAME=dev_fp를 cam_fp로넣고 확인
goto err;
if (ioctl(cam_fp, CMD_CAMERA_INIT, &camif_cfg)) { //모르는 함수
perror("ioctl");
goto err;
}
gettimeofday(&start_tv, &tz); //////모르는 함수
/* Start Camera Codec */
write(cam_fp,"O",2); //에 write 한다
while (!found) { //found에 값이 올때 까지 프레임과 골이 같아 질때 까지
frames++; //프레임을 하나씩 늘려라
save_yuv(cam_width, cam_height, bpp, frames);
if( frames == goal ) { found = 1; break;}
if ((frames % 30) == 0) { // 프레임이 30의 배수 이면
gettimeofday(&end_tv, &tz);
print_fps(&start_tv, &end_tv);
gettimeofday(&start_tv, &tz);
}
}
err:
/* Stop Camera Codec */
write(cam_fp,"X",2);
if (cam_fp) //카메라 파일 포인터가 들어오면 파일 포인터를 닫는다
close(cam_fp);
free(g_yuv); //할당되 메모리 영역 해제
return 0;
}
Forums:


댓글 달기