// разжать картинку(JPEG) IplImage* CameraMJPG::decompress_image(const char* src, int src_size) { if(!src || src_size<=0) return NULL; IplImage* image = NULL; try { CvMat jpeg_mat = cvMat(src_size, 1, CV_8UC1, (void*)src); image = cvDecodeImage(&jpeg_mat); } catch(const std::exception &e) { printf("[!][CameraMJPG][decompress_image] Exception: %s\n", e.what()); image = NULL; } catch(...) { printf("[!][CameraMJPG][decompress_image] Exception in cvDecodeImage()!\n"); image = NULL; throw; } return image; }
void OpenCVPreview::Callback(io::Message msg){ IplImage* frame; CvMat cvmat = cvMat(msg.size.height, msg.size.width, CV_8UC3, msg.buffer); frame = cvDecodeImage(&cvmat, 1); cvNamedWindow("window",CV_WINDOW_AUTOSIZE); cvShowImage("window", frame); cvWaitKey(3); //cvSaveImage("image.jpg", frame, 0); }
// function to decompress 3 channel color IplImages // returns IplImage that needs to be released by the calling function CvMat* Image::decompress_image(IplImage** dst_img, IplImage* src_img) { // convert IplImage to CvMat CvMat matstub, *iplmat; iplmat = cvGetMat(src_img, &matstub, 0, 0); if(iplmat == NULL) return NULL; // decompress the image and add it to dst_img *dst_img = cvDecodeImage(iplmat, CV_LOAD_IMAGE_COLOR); return iplmat; }
// called by the loop to actually grab the frames int capture_image(int fd,CvFont *font, int *set_quality, IplImage* frame,CvMat *cvmat,char *capture_title,v4l2_buffer *buf, uint32_t *start_time, uint32_t *image_count) { // request a new frame if(-1 == xioctl(fd, VIDIOC_QBUF, buf)) { perror("Query Buffer"); return 1; } // wait up to 2 sec for a new frame to arive fd_set fds; FD_ZERO(&fds); FD_SET(fd, &fds); struct timeval tv = {0}; tv.tv_sec = 2; int r = select(fd+1, &fds, NULL, NULL, &tv); if(-1 == r) { perror("Waiting for Frame"); return 1; } // read it if(-1 == xioctl(fd, VIDIOC_DQBUF, buf)) { perror("Retrieving Frame"); return 1; } // convert v4l2 buffer to opencv image *cvmat = cvMat(height, width, CV_8UC3, (void*)buffer); frame = cvDecodeImage(cvmat, 1); // add title, reused tv from select-wait gettimeofday(&tv, NULL); time_t secs = time(0); struct tm *local = localtime(&secs); sprintf(capture_title, CAPTURE_PROTO, local->tm_hour, local->tm_min, local->tm_sec, (int)((unsigned long long)(tv.tv_usec) / 1000)%1000); (*image_count)++; printf("%s @ %2.2f fps\n",capture_title, round((float)(*image_count)*100/(time(0)-(*start_time)))/100 ); cvPutText(frame, capture_title, cvPoint(22, 22), font, cvScalar(0,0,0,0)); cvPutText(frame, capture_title, cvPoint(24, 24), font, cvScalar(200,200,200,0)); // save to disk ... well RAM cvSaveImage("/dev/shm/mjpeg/cam_full.part.jpg", frame, set_quality); rename("/dev/shm/mjpeg/cam_full.part.jpg","/dev/shm/mjpeg/cam_full.jpg"); // important to avoid mem leakage cvReleaseImage(&frame); return 0; }
void LabeledImage::loadImage(string host) { // URL: host/image/id if(image == NULL) { char url[host.length() + 14 + 10]; sprintf(url, "http://%s/image/%u", host.c_str(), id); string curlBuffer = curlGet(url); // Make an imageheader CvMat* matbuf = cvCreateMat(1,curlBuffer.size(),CV_8UC1); memcpy(matbuf->data.ptr, curlBuffer.data(), curlBuffer.size()); // Assign this to an image header image = cvDecodeImage(matbuf); } }
int capture_image(int fd) { struct v4l2_buffer buf = {0}; buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = 0; if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) { perror("Query Buffer"); return 1; } if (-1 == xioctl(fd, VIDIOC_STREAMON, &buf.type)) { perror("Start Capture"); return 1; } fd_set fds; FD_ZERO(&fds); FD_SET(fd, &fds); struct timeval tv = {0}; tv.tv_sec = 2; int r = select(fd+1, &fds, NULL, NULL, &tv); if (-1 == r) { perror("Waiting for Frame"); return 1; } if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) { perror("Retrieving Frame"); return 1; } printf ("saving image\n"); IplImage *frame; CvMat cvmat = cvMat(480, 640, CV_8UC3, (void*)buffer); frame = cvDecodeImage(&cvmat, 1); cvNamedWindow("window",CV_WINDOW_AUTOSIZE); cvShowImage("window", frame); cvWaitKey(0); cvSaveImage(__FILE__ ".jpg", frame, 0); return 0; }
void process_image( char* buffer, int len ) { GPtrArray *markers; int i; CvMat *m = cvCreateMatHeader (len, 1, CV_8UC1); cvSetData (m, buffer, 1); IplImage *color = cvDecodeImage (m, CV_LOAD_IMAGE_COLOR); assert (color != NULL); IplImage *bw = cvCreateImage(cvGetSize(color), color->depth, 1); cvCvtColor (color, bw, CV_RGB2GRAY); koki_camera_params_t params; params.size.x = bw->width; params.size.y = bw->height; params.principal_point.x = params.size.x / 2; params.principal_point.y = params.size.y / 2; params.focal_length.x = 571.0; params.focal_length.y = 571.0; markers = koki_find_markers(koki, bw, 0.11, ¶ms); assert(markers != NULL); for (i=0; i<markers->len; i++){ koki_marker_t *marker; marker = g_ptr_array_index(markers, i); printf("\n(%d) Marker #%d:\n", i, marker->code); } cvRelease ((void**)&bw); cvShowImage ("Source", color); cvWaitKey (0); cvRelease ((void**)&color); cvRelease ((void**)&m); }
void CompactDataLayer<Dtype>::InternalThreadEntry() { Datum datum; string value; CvMat mat; IplImage *img = NULL; CHECK(this->prefetch_data_.count()); Dtype* top_data = this->prefetch_data_.mutable_cpu_data(); Dtype* top_label = NULL; // suppress warnings about uninitialized variables if (this->output_labels_) { top_label = this->prefetch_label_.mutable_cpu_data(); } const int batch_size = this->layer_param_.data_param().batch_size(); for (int item_id = 0; item_id < batch_size; ++item_id) { // get a blob switch (this->layer_param_.data_param().backend()) { case DataParameter_DB_LEVELDB: CHECK(iter_); CHECK(iter_->Valid()); value = iter_->value().ToString(); mat = cvMat(1, 1000 * 1000, CV_8UC1, const_cast<char *>(value.data()) + sizeof(int)); // datum.ParseFromString(iter_->value().ToString()); break; case DataParameter_DB_LMDB: //LOG(FATAL) << "LMDB is not supported at present"; CHECK_EQ(mdb_cursor_get(mdb_cursor_, &mdb_key_, &mdb_value_, MDB_GET_CURRENT), MDB_SUCCESS); mat = cvMat(1, 1000 * 1000 * 3, CV_8UC1, (char *)(mdb_value_.mv_data) + sizeof(int)); // datum.ParseFromArray(mdb_value_.mv_data, // mdb_value_.mv_size); break; default: LOG(FATAL) << "Unknown database backend"; } img = cvDecodeImage(&mat, 0); // Apply data transformations (mirror, scale, crop...) this->data_transformer_.Transform(item_id, img, this->mean_, top_data); cvReleaseImage(&img); // release current image if (this->output_labels_) { //top_label[item_id] = datum.label(); switch(this->layer_param_.data_param().backend()) { case DataParameter_DB_LEVELDB: top_label[item_id] = *((int *)const_cast<char *>(value.data())); break; case DataParameter_DB_LMDB: top_label[item_id] = *((int *)mdb_value_.mv_data); break; default: LOG(FATAL) << "Unkown database backend"; } // LOG(INFO) << "label: " << top_label[item_id]; } // go to the next iter switch (this->layer_param_.data_param().backend()) { case DataParameter_DB_LEVELDB: iter_->Next(); if (!iter_->Valid()) { // We have reached the end. Restart from the first. DLOG(INFO) << "Restarting data prefetching from start."; iter_->SeekToFirst(); } break; case DataParameter_DB_LMDB: if (mdb_cursor_get(mdb_cursor_, &mdb_key_, &mdb_value_, MDB_NEXT) != MDB_SUCCESS) { // We have reached the end. Restart from the first. DLOG(INFO) << "Restarting data prefetching from start."; CHECK_EQ(mdb_cursor_get(mdb_cursor_, &mdb_key_, &mdb_value_, MDB_FIRST), MDB_SUCCESS); } break; default: LOG(FATAL) << "Unknown database backend"; } } }
void CompactDataLayer<Dtype>::DataLayerSetUp(const vector<Blob<Dtype>*>& bottom, vector<Blob<Dtype>*>* top) { // Initialize DB switch (this->layer_param_.data_param().backend()) { case DataParameter_DB_LEVELDB: { leveldb::DB* db_temp; leveldb::Options options = GetLevelDBOptions(); options.create_if_missing = false; LOG(INFO) << "Opening leveldb " << this->layer_param_.data_param().source(); leveldb::Status status = leveldb::DB::Open( options, this->layer_param_.data_param().source(), &db_temp); CHECK(status.ok()) << "Failed to open leveldb " << this->layer_param_.data_param().source() << std::endl << status.ToString(); db_.reset(db_temp); iter_.reset(db_->NewIterator(leveldb::ReadOptions())); iter_->SeekToFirst(); } break; case DataParameter_DB_LMDB: CHECK_EQ(mdb_env_create(&mdb_env_), MDB_SUCCESS) << "mdb_env_create failed"; CHECK_EQ(mdb_env_set_mapsize(mdb_env_, 1099511627776), MDB_SUCCESS); // 1TB CHECK_EQ(mdb_env_open(mdb_env_, this->layer_param_.data_param().source().c_str(), MDB_RDONLY|MDB_NOTLS, 0664), MDB_SUCCESS) << "mdb_env_open failed"; CHECK_EQ(mdb_txn_begin(mdb_env_, NULL, MDB_RDONLY, &mdb_txn_), MDB_SUCCESS) << "mdb_txn_begin failed"; CHECK_EQ(mdb_dbi_open(mdb_txn_, NULL, 0, &mdb_dbi_), MDB_SUCCESS) << "mdb_open failed"; CHECK_EQ(mdb_cursor_open(mdb_txn_, mdb_dbi_, &mdb_cursor_), MDB_SUCCESS) << "mdb_cursor_open failed"; LOG(INFO) << "Opening lmdb " << this->layer_param_.data_param().source(); CHECK_EQ(mdb_cursor_get(mdb_cursor_, &mdb_key_, &mdb_value_, MDB_FIRST), MDB_SUCCESS) << "mdb_cursor_get failed"; break; default: LOG(FATAL) << "Unknown database backend"; } // Check if we would need to randomly skip a few data points if (this->layer_param_.data_param().rand_skip()) { unsigned int skip = caffe_rng_rand() % this->layer_param_.data_param().rand_skip(); LOG(INFO) << "Skipping first " << skip << " data points."; while (skip-- > 0) { switch (this->layer_param_.data_param().backend()) { case DataParameter_DB_LEVELDB: iter_->Next(); if (!iter_->Valid()) { iter_->SeekToFirst(); } break; case DataParameter_DB_LMDB: if (mdb_cursor_get(mdb_cursor_, &mdb_key_, &mdb_value_, MDB_NEXT) != MDB_SUCCESS) { CHECK_EQ(mdb_cursor_get(mdb_cursor_, &mdb_key_, &mdb_value_, MDB_FIRST), MDB_SUCCESS); } break; default: LOG(FATAL) << "Unknown database backend"; } } } // Read a data point, and use it to initialize the top blob. Datum datum; string value; CvMat mat; IplImage *img = NULL; switch (this->layer_param_.data_param().backend()) { case DataParameter_DB_LEVELDB: value = this->iter_->value().ToString(); mat = cvMat(1, 1000 * 1000 * 3, CV_8UC1, const_cast<char *>(value.data()) + sizeof(int)); //datum.ParseFromString(iter_->value().ToString()); break; case DataParameter_DB_LMDB: mat = cvMat(1, 1000 * 1000 * 3, CV_8UC1, (char *)(mdb_value_.mv_data) + sizeof(int)); //datum.ParseFromArray(mdb_value_.mv_data, mdb_value_.mv_size); break; default: LOG(FATAL) << "Unknown database backend"; } img = cvDecodeImage(&mat, 0); // datum size this->datum_channels_ = img->nChannels; cvReleaseImage(&img); // image int crop_size = this->layer_param_.transform_param().crop_size(); CHECK_GT(crop_size, 0) << "crop size must be greater than 0"; (*top)[0]->Reshape(this->layer_param_.data_param().batch_size(), this->datum_channels_ , crop_size, crop_size); this->prefetch_data_.Reshape(this->layer_param_.data_param().batch_size(), this->datum_channels_ , crop_size, crop_size); LOG(INFO) << "output data size: " << (*top)[0]->num() << "," << (*top)[0]->channels() << "," << (*top)[0]->height() << "," << (*top)[0]->width(); // label if (this->output_labels_) { (*top)[1]->Reshape(this->layer_param_.data_param().batch_size(), 1, 1, 1); this->prefetch_label_.Reshape(this->layer_param_.data_param().batch_size(), 1, 1, 1); } this->datum_height_ = crop_size; this->datum_width_ = crop_size; this->datum_size_ = this->datum_channels_ * this->datum_height_ * this->datum_width_; }
/** * buffer header callback function for encoder * * Callback will dump buffer data to the specific file * * @param port Pointer to port from which callback originated * @param buffer mmal buffer header pointer */ static void encoder_buffer_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer) { int complete = 0; // We pass our file handle and other stuff in via the userdata field. PORT_USERDATA *pData = (PORT_USERDATA *)port->userdata; if (pData) { if (buffer->length) { mmal_buffer_header_mem_lock(buffer); // // *** PR : OPEN CV Stuff here ! // // create a CvMat empty structure, with size of the buffer. CvMat* buf = cvCreateMat(1,buffer->length,CV_8UC1); // copy buffer from cam to CvMat buf->data.ptr = buffer->data; // decode image (interpret jpg) IplImage *img = cvDecodeImage(buf, CV_LOAD_IMAGE_COLOR); // we can save it ! cvSaveImage("foobar.bmp", img,0); // or display it cvNamedWindow("camcvWin", CV_WINDOW_AUTOSIZE); cvShowImage("camcvWin", img ); cvWaitKey(0); //cvReleaseImage(&img ); mmal_buffer_header_mem_unlock(buffer); } // Now flag if we have completed if (buffer->flags & (MMAL_BUFFER_HEADER_FLAG_FRAME_END | MMAL_BUFFER_HEADER_FLAG_TRANSMISSION_FAILED)) complete = 1; } else { vcos_log_error("Received a encoder buffer callback with no state"); } // release buffer back to the pool mmal_buffer_header_release(buffer); // and send one back to the port (if still open) if (port->is_enabled) { MMAL_STATUS_T status; MMAL_BUFFER_HEADER_T *new_buffer; new_buffer = mmal_queue_get(pData->pstate->encoder_pool->queue); if (new_buffer) { status = mmal_port_send_buffer(port, new_buffer); } if (!new_buffer || status != MMAL_SUCCESS) vcos_log_error("Unable to return a buffer to the encoder port"); } if (complete) vcos_semaphore_post(&(pData->complete_semaphore)); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //show rectangle boxes(with object_tracking result) void show_rects(IplImage *Image,RESULT *CUR,FLOAT ratio) { #if 0 //parameters const int height = Image->height; const int width = Image->width; const int UpY = 0; const int NEW_Y = Image->height; for(int ii=0;ii<CUR->num;ii++) { //int *P = CUR->point+4*ii; int *P = CUR->OR_point+4*ii; CvScalar col = get_color(CUR->type[ii]); CvPoint p1=cvPoint(P[0],P[1]); CvPoint p2=cvPoint(P[2],P[3]); cvRectangle(Image,p1,p2,col,3); //draw current-object rectangle cvLine(Image,p1,p2,col,2); p1 = cvPoint(P[0],P[3]); p2 = cvPoint(P[2],P[1]); cvLine(Image,p1,p2,col,2); } #else //parameters const int height = Image->height; const int width = Image->width; const int UpY = 0; const int NEW_Y = Image->height; // generate key // key_t shm_key = ftok(OUTPUT_SHM_PATH, 1); // if(shm_key == -1) { // printf("key generation for output_SHM is failed\n"); // } key_t shm_key_height = ftok(HEIGHT_SHM_PATH, 1); if(shm_key_height == -1) { printf("key generation for height_SHM is failed\n"); } key_t shm_key_width = ftok(WIDTH_SHM_PATH, 1); if(shm_key_width == -1) { printf("key generation for width_SHM is failed\n"); } // key_t shm_key_rbuf_dst = ftok(RBUF_DST_PATH, 1); // if(shm_key_rbuf_dst == -1) { // printf("key generation for rbuf_dst_SHM is failed\n"); // } key_t shm_key_rbuf = ftok(RBUF_PATH, 1); if(shm_key_rbuf == -1) { printf("key generation for rbuf_SHM is failed\n"); } key_t shm_key_rbuf_head = ftok(RBUF_HEAD_PATH, 1); if(shm_key_rbuf_head == -1) { printf("key generation for rbuf_head_SHM is failed\n"); } key_t shm_key_rbuf_tail = ftok(RBUF_TAIL_PATH, 1); if(shm_key_rbuf_tail == -1) { printf("key generation for rbuf_tail_SHM is failed\n"); } // generate key for semaphore key_t sem_key = ftok(SEM_PATH, 1); // key for semaphore if(sem_key == -1) { // error semantics printf("key heneration for semaphore is failed\n"); } /* generate key for fps keeper */ key_t shm_key_pds_fps = ftok(PDS_FPS_SHM_PATH, 1); if(shm_key_pds_fps == -1) { printf("key generation for pds_fps_SHM is failed\n"); } // access to the shared memory // int shrd_id = shmget(shm_key, IMAGE_SIZE, 0666); // if(shrd_id < 0) { // printf("Can't Access to the Shared Memory!! \n"); // } int shrd_id_height = shmget(shm_key_height, sizeof(int), 0666); if(shrd_id_height < 0) { printf("Can't Access to the Shared Memory!! \n"); } int shrd_id_width = shmget(shm_key_width, sizeof(int), 0666); if(shrd_id_width < 0) { printf("Can't Access to the Shared Memory!! \n"); } // int shrd_id_rbuf_dst = shmget(shm_key_rbuf_dst, RBUF_ELEMENT_NUM*sizeof(int), 0666); // if(shrd_id_rbuf_dst < 0) { // printf("Can't Access to the Shared Memory!! \n"); // } // int shrd_id_rbuf = shmget(shm_key_rbuf, MAX_OBJECT_NUM*sizeof(int*), 0666); int shrd_id_rbuf = shmget(shm_key_rbuf, MAX_OBJECT_NUM*sizeof(obj_coordinate), 0666); if(shrd_id_rbuf < 0) { printf("Can't Access to the Shared Memory!! \n"); } int shrd_id_rbuf_head = shmget(shm_key_rbuf_head, sizeof(int), 0666); if(shrd_id_rbuf_head < 0) { printf("Can't Access to the Shared Memory!! \n"); } int shrd_id_rbuf_tail = shmget(shm_key_rbuf_tail, sizeof(int), 0666); if(shrd_id_rbuf_tail < 0) { printf("Can't Access to the Shared Memory!! \n"); } // open semaphore int semid = semget(sem_key, 1, 0666); if(semid == -1) { printf("Can't Access to the semaphore\n"); } /* access to shared fps keeper */ int shrd_id_pds_fps = shmget(shm_key_pds_fps, sizeof(int), 0666); if(shrd_id_pds_fps < 0) { printf("Can't Access to the Shared Memory!! \n"); } // unsigned char *shrd_ptr = (unsigned char *)shmat(shrd_id, NULL, 0); int *shrd_ptr_height = (int *)shmat(shrd_id_height, NULL, 0); int *shrd_ptr_width = (int *)shmat(shrd_id_width, NULL, 0); // int *shrd_ptr_rbuf_dst = (int *)shmat(shrd_id_rbuf_dst, NULL, 0); // int **shrd_ptr_rbuf = (int **)shmat(shrd_id_rbuf, NULL, 0); obj_coordinate *shrd_ptr_rbuf = (obj_coordinate *)shmat(shrd_id_rbuf, NULL, 0); int *shrd_ptr_rbuf_head = (int *)shmat(shrd_id_rbuf_head, NULL, 0); int *shrd_ptr_rbuf_tail = (int *)shmat(shrd_id_rbuf_tail, NULL, 0); int *shrd_ptr_pds_fps = (int *)shmat(shrd_id_pds_fps, NULL, 0); // int *tmpptr = shrd_ptr_rbuf_dst; // for(int i=0; i<MAX_OBJECT_NUM; i++) { // shrd_ptr_rbuf[i] = tmpptr; // tmpptr += CO_NUM; // } #if 0 // IplImage *output_image = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3); IplImage *output_image = cvCreateImage(cvSize(*shrd_ptr_width, *shrd_ptr_height), IPL_DEPTH_8U, 3); // output_image->imageData = (char *)shrd_ptr; /* for bitmap image, set the point of origin of image to left below */ output_image->origin = 1; /* skip header information */ shrd_ptr += HEADER_SIZE; /* To keep original data, use copied image data */ // memcpy(output_image->imageData, shrd_ptr, IMAGE_SIZE); output_image->imageData = (char *)shrd_ptr; #endif #if 0 /* read image from buffer */ CvMat *buf = cvCreateMat(1, IMAGE_SIZE, CV_8UC3); My_sem_operation(semid, LOCK); // lock semaphore // buf->data.ptr = shrd_ptr; memcpy(buf->data.ptr, shrd_ptr, IMAGE_SIZE); My_sem_operation(semid, UNLOCK); // unlock semaphore IplImage *output_image = cvDecodeImage(buf, CV_LOAD_IMAGE_COLOR); // absorb the difference of file format #endif for(int ii=0;ii<CUR->num;ii++) { //int *P = CUR->point+4*ii; int *P = CUR->OR_point+4*ii; CvScalar col = get_color(CUR->type[ii]); CvPoint p1=cvPoint(P[0],P[1]); CvPoint p2=cvPoint(P[2],P[3]); // // draw rectangle to original image // cvRectangle(Image,p1,p2,col,3); //draw current-object rectangle // cvLine(Image,p1,p2,col,2); // draw rectangle to shared memory image // cvRectangle(output_image,p1,p2,col,3); //draw current-object rectangle // cvLine(output_image,p1,p2,col,2); p1 = cvPoint(P[0],P[3]); p2 = cvPoint(P[2],P[1]); // // draw rectangle to original image // cvLine(Image,p1,p2,col,2); // draw rectangle to shared memory image // cvLine(output_image,p1,p2,col,2); /* write coodinates to ring buffer*/ My_sem_operation(semid, LOCK); // lock semaphore // apSetCoordinate(shrd_ptr_rbuf, P[0], shrd_ptr_rbuf_head, shrd_ptr_rbuf_tail, LEFT); // apSetCoordinate(shrd_ptr_rbuf, P[1], shrd_ptr_rbuf_head, shrd_ptr_rbuf_tail, UPPER); // apSetCoordinate(shrd_ptr_rbuf, P[2], shrd_ptr_rbuf_head, shrd_ptr_rbuf_tail, RIGHT); // apSetCoordinate(shrd_ptr_rbuf, P[3], shrd_ptr_rbuf_head, shrd_ptr_rbuf_tail, BOTTOM); apSetCoordinate(shrd_ptr_rbuf, // obj_coordinate *queue shrd_ptr_rbuf_head, // int *head shrd_ptr_rbuf_tail, // int *tail P[0], // int left P[1], // int upper P[2], // int right P[3], // int bottom // CAR // int type PEDESTRIAN // int type ); My_sem_operation(semid, UNLOCK); // unlock semaphore } /* increment frame per second */ My_sem_operation(semid, LOCK); // lock semaphore *shrd_ptr_pds_fps += 1; My_sem_operation(semid, UNLOCK); // unlock semaphore #if 0 /* copy back to the shared memory by png format*/ CvMat *buf_for_output = cvEncodeImage(".png", output_image); //CvMat *buf_for_output = cvEncodeImage(".bmp", output_image); //CvMat *buf_for_output = cvEncodeImage(".jpeg", output_image); My_sem_operation(semid, LOCK); // lock semaphore // memcpy(shrd_ptr, buf_for_output->data.ptr, IMAGE_SIZE); // サイズ違いでエラー? memcpy(shrd_ptr, buf_for_output->data.ptr, (buf_for_output->rows)*(buf_for_output->cols)*sizeof(unsigned char)); // サイズ違いでエラー? My_sem_operation(semid, UNLOCK); // unlock semaphore cvShowImage("for debug", output_image); cvWaitKey(10); #endif #if 0 FILE *testoutput = fopen("./testoutput", "wb"); if(testoutput == NULL){ printf("test output error\n"); } unsigned char *tmpptr = (unsigned char *)buf_for_output->data.ptr; for(int i=0; i<IMAGE_SIZE; i++) { fprintf(testoutput, "%c",*tmpptr); tmpptr++; } fclose(testoutput); #endif /* detouch(purge) shared memory */ // if(shmdt(shrd_ptr)==-1){ // printf("purge error! (shrd_ptr)\n"); // } if(shmdt(shrd_ptr_height)==-1){ printf("purge error! (shrd_ptr_height)\n"); } if(shmdt(shrd_ptr_width)==-1){ printf("purge error! (shrd_ptr_width)\n"); } // if(shmdt(shrd_ptr_rbuf_dst)==-1) { // printf("purge error! (shrd_ptr_rbuf_dst)\n"); // } if(shmdt(shrd_ptr_rbuf)==-1) { printf("purge error! (shrd_ptr_rbuf)\n"); } if(shmdt(shrd_ptr_rbuf_head)==-1) { printf("purge error! (shrd_ptr_rbuf_head)\n"); } if(shmdt(shrd_ptr_rbuf_tail)==-1) { printf("purge error! (shrd_ptr_rbuf_tail)\n"); } if(shmdt(shrd_ptr_pds_fps)==-1){ printf("purge error! (shrd_ptr_pds_fps)\n"); } #if 0 /* release image */ cvReleaseImage(&output_image); cvReleaseMat(&buf); cvReleaseMat(&buf_for_output); #endif #endif }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //load_successive_image IplImage *load_suc_image(int fnum) { #if 0 char pass[MAXLINE]; char num[8]; //strcpy_s(pass,sizeof(pass),IN_S_NAME); strcpy(pass, IN_S_NAME); //sprintf_s(num,sizeof(num),"%d",fnum); sprintf(num, "%d",fnum); //strcat_s(pass,sizeof(pass),num); strcat(pass, num); //strcat_s(pass,sizeof(pass),EX_NAME); strcat(pass, EX_NAME); printf("%s\n",pass); return(cvLoadImage(pass,CV_LOAD_IMAGE_COLOR)); #else /*****************************************************/ // generate key /*****************************************************/ key_t shm_key = ftok(INPUT_SHM_PATH, 1); if(shm_key == -1) { printf("key generation for input_SHM is failed\n"); } key_t shm_key_height = ftok(HEIGHT_SHM_PATH, 1); if(shm_key == -1) { printf("key generation for height_SHM is failed\n"); } key_t shm_key_width = ftok(WIDTH_SHM_PATH, 1); if(shm_key == -1) { printf("key generation for width_SHM is failed\n"); } // generation key for semaphore key_t sem_key = ftok(SEM_PATH, 1); // key for semaphore if(sem_key == -1) { printf("key generation for semaphore is failed\n"); } // generation key for reader-writer lock key_t shm_key_rwlock = ftok(RWLOCK_SHM_PATH, 1); if(shm_key_rwlock == -1) { printf("key generation for reader-writer lock failed\n"); } // key generation for image update checker key_t shm_key_imgupd = ftok(IMGUPD_SHM_PATH, 1); if(shm_key_imgupd == -1) { // error semantics printf("generation key for image update checker failed\n"); } /*****************************************************/ // access to the shared memory /*****************************************************/ int shrd_id = shmget(shm_key, IMAGE_SIZE, 0666); if(shrd_id < 0) { printf("Can't Access to the Shared Memory!! \n"); } int shrd_id_height = shmget(shm_key_height, sizeof(int), 0666); if(shrd_id_height < 0) { printf("Can't Access to the Shared Memory!! \n"); } int shrd_id_width = shmget(shm_key_width, sizeof(int), 0666); if(shrd_id_width < 0) { printf("Can't Access to the Shared Memory!! \n"); } // open semaphore int semid = semget(sem_key, 1, 0666); if(semid == -1) { printf("Can't Access to the semaphore\n"); } // open reader-writer lock int shrd_id_rwlock = shmget(shm_key_rwlock, sizeof(pthread_rwlock_t), 0666); // access shared image update checker int shrd_id_imgupd = shmget(shm_key_imgupd, sizeof(char)*256, 0666); if(shrd_id_imgupd < 0) { // error semantics printf("Can't Access Shared memory for image update checker...\n"); } unsigned char *shrd_ptr = (unsigned char*)shmat(shrd_id, NULL, 0); int *shrd_ptr_height = (int*)shmat(shrd_id_height, NULL, 0); int *shrd_ptr_width = (int*)shmat(shrd_id_width, NULL, 0); // attach reader-writer lock pthread_rwlock_t *shrd_ptr_rwlock = (pthread_rwlock_t *)shmat(shrd_id_rwlock, NULL, 0); // attach image update checker char *shrd_ptr_imgupd = (char*)shmat(shrd_id_imgupd, NULL, 0); #if 0 // IplImage *image = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3); IplImage *image = cvCreateImage(cvSize(*shrd_ptr_width, *shrd_ptr_height), IPL_DEPTH_8U, 3); /* for bitmap image, set the point of origin of image to left below */ image->origin = 1; /* skip header information */ shrd_ptr += HEADER_SIZE; /* To keep original data, use copied image data */ // memcpy(image->imageData, shrd_ptr, IMAGE_SIZE); image->imageData = (char *)shrd_ptr; #endif // image update check static char imgupd_before[256] = {0}; int upd_check = 0; while(1) { My_sem_operation(semid, LOCK); // lock semaphore upd_check = strcmp(shrd_ptr_imgupd, imgupd_before); My_sem_operation(semid, UNLOCK); // unlock semaphore // if shrd_ptr_imgupd == imgupd_before, then continue loop if(upd_check != 0) { My_sem_operation(semid, LOCK); // lock semaphore strcpy(imgupd_before, shrd_ptr_imgupd); My_sem_operation(semid, UNLOCK); // unlock semaphore break; } } /* read image from buffer */ CvMat *buf = cvCreateMat(1, IMAGE_SIZE, CV_8UC3); // My_sem_operation(semid, LOCK); // lock semaphore int rtn = pthread_rwlock_rdlock(shrd_ptr_rwlock); // lock reader-writer lock as reader if(rtn != 0) { printf("pthread_rwlock_rdlock failed...\n"); } memcpy(buf->data.ptr, shrd_ptr, IMAGE_SIZE); rtn = pthread_rwlock_unlock(shrd_ptr_rwlock); // unlock reader-writer lock if(rtn != 0) { printf("pthread_rwlock_unlock failed...\n"); } // My_sem_operation(semid, UNLOCK); // unlock semaphore IplImage *image = cvDecodeImage(buf, CV_LOAD_IMAGE_COLOR); // absorb the difference of file format // test: output shared memory data // cvNamedWindow("test output", CV_WINDOW_AUTOSIZE); // cvShowImage("test output", image); // cvWaitKey(0); return(image); #endif }
/** * main */ int main(int argc, const char **argv) { // *** MODIFICATION: OpenCV modifications. // Load previous image. IplImage* prevImage = cvLoadImage("motion1.jpg", CV_LOAD_IMAGE_COLOR); // Create two arrays with the same number of channels than the original one. avg1 = cvCreateMat(prevImage->height,prevImage->width,CV_32FC3); avg2 = cvCreateMat(prevImage->height,prevImage->width,CV_32FC3); // Create image of 32 bits. IplImage* image32 = cvCreateImage(cvSize(prevImage->width,prevImage->height), 32,3); // Convert image to 32 bits. cvConvertScale(prevImage,image32,1/255,0); // Set data from previous image into arrays. cvSetData(avg1,image32->imageData,image32->widthStep); cvSetData(avg2,image32->imageData,image32->widthStep); // *** MODIFICATION end // Our main data storage vessel.. RASPISTILL_STATE state; MMAL_STATUS_T status = MMAL_SUCCESS; MMAL_PORT_T *camera_preview_port = NULL; MMAL_PORT_T *camera_video_port = NULL; MMAL_PORT_T *camera_still_port = NULL; MMAL_PORT_T *preview_input_port = NULL; MMAL_PORT_T *encoder_input_port = NULL; MMAL_PORT_T *encoder_output_port = NULL; bcm_host_init(); // Register our application with the logging system vcos_log_register("fast", VCOS_LOG_CATEGORY); signal(SIGINT, signal_handler); default_status(&state); if (state.verbose) { fprintf(stderr, "\n%s Camera App %s\n\n", basename(argv[0]), VERSION_STRING); } // OK, we have a nice set of parameters. Now set up our components // We have three components. Camera, Preview and encoder. // Camera and encoder are different in stills/video, but preview // is the same so handed off to a separate module if ((status = create_camera_component(&state)) != MMAL_SUCCESS) { vcos_log_error("%s: Failed to create camera component", __func__); } else if ((status = raspipreview_create(&state.preview_parameters)) != MMAL_SUCCESS) { vcos_log_error("%s: Failed to create preview component", __func__); destroy_camera_component(&state); } else if ((status = create_encoder_component(&state)) != MMAL_SUCCESS) { vcos_log_error("%s: Failed to create encode component", __func__); raspipreview_destroy(&state.preview_parameters); destroy_camera_component(&state); } else { PORT_USERDATA callback_data; if (state.verbose) fprintf(stderr, "Starting component connection stage\n"); camera_preview_port = state.camera_component->output[MMAL_CAMERA_PREVIEW_PORT]; camera_video_port = state.camera_component->output[MMAL_CAMERA_VIDEO_PORT]; camera_still_port = state.camera_component->output[MMAL_CAMERA_CAPTURE_PORT]; preview_input_port = state.preview_parameters.preview_component->input[0]; encoder_input_port = state.encoder_component->input[0]; encoder_output_port = state.encoder_component->output[0]; if (state.preview_parameters.wantPreview ) { if (state.verbose) { fprintf(stderr, "Connecting camera preview port to preview input port\n"); fprintf(stderr, "Starting video preview\n"); } // *** USER: remove preview // Connect camera to preview //status = connect_ports(camera_preview_port, preview_input_port, &state.preview_connection); } else { status = MMAL_SUCCESS; } if (status == MMAL_SUCCESS) { VCOS_STATUS_T vcos_status; if (state.verbose) fprintf(stderr, "Connecting camera stills port to encoder input port\n"); // Now connect the camera to the encoder status = connect_ports(camera_still_port, encoder_input_port, &state.encoder_connection); if (status != MMAL_SUCCESS) { vcos_log_error("%s: Failed to connect camera video port to encoder input", __func__); goto error; } // Set up our userdata - this is passed though to the callback where we need the information. // Null until we open our filename callback_data.file_handle = NULL; callback_data.pstate = &state; vcos_status = vcos_semaphore_create(&callback_data.complete_semaphore, "RaspiStill-sem", 0); vcos_assert(vcos_status == VCOS_SUCCESS); if (status != MMAL_SUCCESS) { vcos_log_error("Failed to setup encoder output"); goto error; } FILE *output_file = NULL; int frame = 1; // Enable the encoder output port encoder_output_port->userdata = (struct MMAL_PORT_USERDATA_T *)&callback_data; if (state.verbose) fprintf(stderr, "Enabling encoder output port\n"); // Enable the encoder output port and tell it its callback function status = mmal_port_enable(encoder_output_port, encoder_buffer_callback); // Create an empty matrix with the size of the buffer. CvMat* buf = cvCreateMat(1,60000,CV_8UC1); // Keep buffer that gets frames from queue. MMAL_BUFFER_HEADER_T *buffer; // Image to be displayed. IplImage* image; // Keep number of buffers and index for the loop. int num, q; while(1) { // Send all the buffers to the encoder output port num = mmal_queue_length(state.encoder_pool->queue); for (q=0;q<num;q++) { buffer = mmal_queue_get(state.encoder_pool->queue); if (!buffer) vcos_log_error("Unable to get a required buffer %d from pool queue", q); if (mmal_port_send_buffer(encoder_output_port, buffer)!= MMAL_SUCCESS) vcos_log_error("Unable to send a buffer to encoder output port (%d)", q); } // for if (mmal_port_parameter_set_boolean(camera_still_port, MMAL_PARAMETER_CAPTURE, 1) != MMAL_SUCCESS) vcos_log_error("%s: Failed to start capture", __func__); else { // Wait for capture to complete // For some reason using vcos_semaphore_wait_timeout sometimes returns immediately with bad parameter error // even though it appears to be all correct, so reverting to untimed one until figure out why its erratic vcos_semaphore_wait(&callback_data.complete_semaphore); if (state.verbose) fprintf(stderr, "Finished capture %d\n", frame); } // else // Copy buffer from camera to matrix. buf->data.ptr = buffer->data; // This workaround is needed for the code to work // *** TODO: investigate why. printf("Until here works\n"); // Decode the image and display it. image = cvDecodeImage(buf, CV_LOAD_IMAGE_COLOR); // Destinations CvMat* res1 = cvCreateMat(image->height,image->width,CV_8UC3); CvMat* res2 = cvCreateMat(image->height,image->width,CV_8UC3); // Update running averages and then scale, calculate absolute values // and convert the result 8-bit. // *** USER:change the value of the weight. cvRunningAvg(image,avg2,0.0001, NULL); cvConvertScaleAbs(avg2, res2, 1,0); cvRunningAvg(image,avg1,0.1, NULL); cvConvertScaleAbs(avg1, res1, 1,0); // Show images cvShowImage("img",image); cvShowImage("avg1",res1); cvShowImage("avg2",res2); cvWaitKey(20); // Update previous image. cvSaveImage("motion1.jpg", image, 0); } // end while vcos_semaphore_delete(&callback_data.complete_semaphore); } else { mmal_status_to_int(status); vcos_log_error("%s: Failed to connect camera to preview", __func__); } error: mmal_status_to_int(status); if (state.verbose) fprintf(stderr, "Closing down\n"); // Disable all our ports that are not handled by connections check_disable_port(camera_video_port); check_disable_port(encoder_output_port); if (state.preview_parameters.wantPreview ) mmal_connection_destroy(state.preview_connection); mmal_connection_destroy(state.encoder_connection); /* Disable components */ if (state.encoder_component) mmal_component_disable(state.encoder_component); if (state.preview_parameters.preview_component) mmal_component_disable(state.preview_parameters.preview_component); if (state.camera_component) mmal_component_disable(state.camera_component); destroy_encoder_component(&state); raspipreview_destroy(&state.preview_parameters); destroy_camera_component(&state); if (state.verbose) fprintf(stderr, "Close down completed, all components disconnected, disabled and destroyed\n\n"); } if (status != MMAL_SUCCESS) raspicamcontrol_check_configuration(128); return 0; }
int capture_and_send_image(int fd) { enum v4l2_buf_type type; int frame_num = 0; char camActive = 0; char rec; struct v4l2_buffer buf = {0}; int n,i; char *bufferptr; int framesize; long long start, end; for (i = 0; i < n_buffers; ++i) { struct v4l2_buffer buf; CLEAR(buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = i; if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) printf("VIDIOC_QBUF"); } type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (-1 == xioctl(fd, VIDIOC_STREAMON, &type)) printf("VIDIOC_STREAMON"); // buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; // buf.memory = V4L2_MEMORY_MMAP; // buf.index = 0; cvNamedWindow("window",CV_WINDOW_AUTOSIZE); while(1){ if(frame_num == 0){ start = getSystemTime(); } fd_set fds; struct timeval tv; int r; FD_ZERO(&fds); FD_SET(fd, &fds); tv.tv_sec = 2; tv.tv_usec = 0; r = select(fd+1, &fds, NULL, NULL, &tv); if(-1 == r) { perror("Waiting for Frame"); return 1; } CLEAR(buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; if(-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) { switch (errno) { case EAGAIN: continue; case EIO: /* Could ignore EIO, see spec. */ /* fall through */ default: perror("VIDIOC_DQBUF"); } } assert(buf.index < n_buffers); IplImage* frame; CvMat cvmat = cvMat(height, width, CV_8UC3, (void*)buffers[buf.index].start); frame = cvDecodeImage(&cvmat, 1); cvShowImage("window", frame); cvReleaseImage(&frame); cvWaitKey(1); if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) printf("VIDIOC_QBUF"); frame_num++; if(frame_num == 10){ end = getSystemTime(); frame_num = 0; printf("\rFPS:%3lld", 10000/(end - start)); fflush(stdout); } } return 0; }
int memcpy_picture(unsigned char *out, unsigned char *buf, int size) { // video 장치 공유 문제(opecv + mjpg)와 해상도에 따른 성능 문제로 인해 // opencv 해상도(800*600)와 stream(160*120) 해상도 변경 { if(!g_fp) { g_fp = fopen("/dev/shm/picam.jpg", "wb"); if(!g_fp) exit(1); } int fd = fileno(g_fp); while(1) { if(flock(fd, LOCK_EX) < 0) { if(errno == EINTR) continue; } break; } fseek(g_fp, 0, SEEK_SET); fwrite(buf, sizeof(unsigned char), size, g_fp); fflush(g_fp); while(1) { if(flock(fd, LOCK_UN) < 0) { if(errno == EINTR) continue; } break; } /* // 캡쳐된 원본은 object detection에 사용 FILE* fp = fopen("/dev/shm/picam.jpg", "wb"); if(fp) { fwrite(buf, sizeof(unsigned char), size, fp); fclose(fp); } */ // 스케일된 이미지는 스트리밍에 사용 // 이미지 스케일링은 output_http에서 하도록 변경 #if 0 CvMat cm = cvMat(1, size, CV_8UC1, buf); IplImage* src = cvDecodeImage(&cm, CV_LOAD_IMAGE_COLOR); if(!src) return 0; IplImage* dst = cvCreateImage(cvSize(320, 240), src->depth, src->nChannels); if(!dst) return 0; cvResize(src, dst, CV_INTER_LINEAR); //cvSaveImage("/dev/shm/small.jpg", dst, 0); CvMat* enc = cvEncodeImage(".jpg", dst, 0); if(!enc) return 0; printf("#1 buf=%p, size=%d\n", buf, size); printf("enc->data.ptr=%p, enc->rows=%d, enc->cols=%d\n", enc->data.ptr, enc->rows, enc->cols); memcpy(buf, enc->data.ptr, enc->rows * enc->cols); size = enc->rows * enc->cols; printf("#2 buf=%p, size_%d\n", buf, size); cvReleaseMat(&enc); cvReleaseImage(&dst); cvReleaseImage(&src); #endif } unsigned char *ptdeb, *ptlimit, *ptcur = buf; int sizein, pos = 0; if(!is_huffman(buf)) { ptdeb = ptcur = buf; ptlimit = buf + size; while((((ptcur[0] << 8) | ptcur[1]) != 0xffc0) && (ptcur < ptlimit)) ptcur++; if(ptcur >= ptlimit) return pos; sizein = ptcur - ptdeb; memcpy(out + pos, buf, sizein); pos += sizein; memcpy(out + pos, dht_data, sizeof(dht_data)); pos += sizeof(dht_data); memcpy(out + pos, ptcur, size - sizein); pos += size - sizein; } else { memcpy(out + pos, ptcur, size); pos += size; } return pos; }
int main(){ int landmarkId; string imageNameMd5,imageEncoded,imageData; double ***G=NULL; double **g = NULL; IplImage * image = NULL; CvMat * encodedMat = NULL; //IplImage* image =cvLoadImage("test.jpg"); // This is used for computing int imageSize = 128; IplImage *img = NULL; int numberBlocks = 4; int orientationsPerScale[]= {8,8,4}; createGabor(G,orientationsPerScale,imageSize); while (cin>>landmarkId>>imageNameMd5>>imageEncoded){ try { imageData.assign(Base64Decode(imageEncoded.c_str(),imageEncoded.length())); encodedMat = cvCreateMat(1,imageData.length(),CV_8UC1); encodedMat->data.ptr = (uchar *)imageData.c_str(); image = cvDecodeImage(encodedMat); } catch(...){ cvReleaseImage(&image); cvReleaseMat(&encodedMat); } if (!image){ cvReleaseImage(&image); cvReleaseMat(&encodedMat); continue; } //-----------------resize the pic to 128*128-------- CvSize sz; sz.height = imageSize; //128 sz.width = imageSize; img = cvCreateImage(sz,image->depth,image->nChannels); cvResize(image,img,CV_INTER_CUBIC); //-----------------resize the pic to 128*128-------- if (!img) { cvReleaseImage(&image); cvReleaseMat(&encodedMat); cvReleaseImage(&img); //continue; } myImg output = prefilt(img,4); gistGabor(output,numberBlocks,G,imageSize,ssum(orientationsPerScale),g); cout<<landmarkId<<"L"<<imageNameMd5<<"\t"; cout<<sizeof(g); for (int i=0;i<960;i++) cout<<setprecision(4)<<g[i][0]<<'X'; cvReleaseImage(&image); cvReleaseMat(&encodedMat); cvReleaseImage(&img); output.destroy(); cout<<endl; } return 0; }