Ejemplo n.º 1
0
RTC::ReturnCode_t JpegEncoder::onExecute(RTC::UniqueId ec_id)
{
    //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
  if (m_decodedIn.isNew()){
      m_decodedIn.read();

      Img::ImageData& idat = m_decoded.data.image;
      std::vector<uchar>buf;
      std::vector<int> param = std::vector<int>(2);
      param[0] = CV_IMWRITE_JPEG_QUALITY;
      param[1] = m_quality;

      switch(idat.format){
      case Img::CF_RGB:
	{
	  // RGB -> BGR
	  uchar r,g,b, *raw=idat.raw_data.get_buffer();
	  for (unsigned int i=0; i<idat.raw_data.length(); i+=3, raw+=3){
	    r = raw[0]; g = raw[1]; b = raw[2];
	    raw[0] = b; raw[1] = g; raw[2] = r;
	  }
	  
	  cv::Mat src(idat.height, idat.width, CV_8UC3, 
		      idat.raw_data.get_buffer());
	  imencode(".jpg", src, buf, param);
	  m_encoded.data.image.format = Img::CF_RGB_JPEG;
	}
	break;
      case Img::CF_GRAY:
	{
	  cv::Mat src(idat.height, idat.width, CV_8U, 
		      idat.raw_data.get_buffer());
	  imencode(".jpg", src, buf, param);
	  m_encoded.data.image.format = Img::CF_GRAY_JPEG;
	}
	break;
      }
      m_encoded.data.image.raw_data.length(buf.size());
      unsigned char *dst = m_encoded.data.image.raw_data.get_buffer();
      memcpy(dst, &buf[0], buf.size());

#if 0
      std::cout << "JpegEncoder:" << idat.raw_data.length() << "->"
		<< m_encoded.data.image.raw_data.length() << std::endl;
#endif
      m_encodedOut.write();
  }
  return RTC::RTC_OK;
}
Ejemplo n.º 2
0
void V4fWriter::AddFrame(cv::Mat frame)
{
	if (!frame.empty() && Opened && CURRENT_POSITION().Value().abs_coord > 0)
	{
		try
		{
			counterFrames++;
			if(counterFrames != MISSED_FRAMES)
				return;
			counterFrames = -1;
			vector<int> p;
			p.push_back(CV_IMWRITE_JPEG_QUALITY);
			p.push_back(50);
			vector<unsigned char> buf;
			imencode(".jpg", frame, buf, p);
			//—охран¤ем информацию о кадре
			if(dataSet)
			{
				if(current_start_time!=CURRENT_POSITION().Value().start_time)
				{
					this->Open();
				}
				int bufLen = buf.size();
				V4fFrame new_frame(CURRENT_DPP().Value(), CURRENT_POSITION().Value().abs_coord, frame.cols, frame.rows, buf);
				dataSet->WriteFrame(new_frame.dpp, new_frame.absCoord, ENCODE_CV_50, new_frame.height, new_frame.width, new_frame.img.data(), new_frame.img.size());
			}
		}
		catch (std::exception e)
		{
			LOG_ERROR(L"ќшибка при записи кадра cv::Mat (ќбзорное видеонаблюдение)");
		}
	}
}
Ejemplo n.º 3
0
//JNIEXPORT jbyteArray JNICALL Java_com_ocr_jni_Mser_detectIdNum
JNIEXPORT jbyteArray JNICALL Java_org_smirkcat_plateocr_Mser_detectIdNum
(JNIEnv *env, jobject obj, jbyteArray image){
	jboolean isCopy = JNI_FALSE;
	int size = env->GetArrayLength(image);
	jbyte* imagebuffer = env->GetByteArrayElements(image, &isCopy);
	if (NULL == imagebuffer)
	{
		return NULL;
	}
	vector<uchar> outputdata;
	Mser fun;
	Mat src = imdecode(Mat(1, size, CV_8U, imagebuffer), IMREAD_COLOR);
	Mat dst = fun.detectNumber(src);

	if (!dst.data)
		return NULL;

	env->ReleaseByteArrayElements(image, imagebuffer, 0);
	imencode(".bmp", dst, outputdata);

	jbyteArray outarray = env->NewByteArray(outputdata.size());
	env->SetByteArrayRegion(outarray, 0, outputdata.size(), (jbyte*)&outputdata[0]);

	return outarray;
}
Ejemplo n.º 4
0
Archivo: Job.cpp Proyecto: sijp/NASH
		/*
		 * uploads the edited image to the urlUpload
		 */
		void Job::upload(Employee &employee , HttpLineInterperter *resConfiguration)
		{
			string uploadRequest = "PUT /photos/" +
					resConfiguration->getResource() +
					"?rep=" + this->repUpload +
					" HTTP/1.1";
			employee.send(uploadRequest);
			employee.send("Host: " + resConfiguration->getServer());
			employee.send("Content-Type: " + this->mimeType);
			//employee.send("Content-Length: " + this->editedImage.elemSize());
			/*
			 * sending the edited image in bytes
			 */
			int pos = this->mimeType.find("/");
			string imgExt = "."+this->mimeType.substr(pos+1);
			vector<uchar> vecByte;
			imencode(imgExt , this->editedImage , vecByte);
			
			stringstream out;
			out << vecByte.size();
			string clen=out.str();
			cout<<"clen:"<<clen<<endl;
			employee.send("Content-Length: " + clen);
			employee.send("");
			
			uchar* buf = vecByte.data();
			employee.sendBytes(buf , vecByte.size());
		}
Ejemplo n.º 5
0
void calcDenseFlow(string file_name, int bound, int type, int step,
                   vector<vector<uchar> >& output_x,
                   vector<vector<uchar> >& output_y,
                   vector<vector<uchar> >& output_img){

    VideoCapture video_stream(file_name);
    CHECK(video_stream.isOpened())<<"Cannot open video stream \""
                                  <<file_name
                                  <<"\" for optical flow extraction.";

    Mat capture_frame, capture_image, prev_image, capture_gray, prev_gray;
    Mat flow, flow_split[2];


    bool initialized = false;
    for(int iter = 0;; iter++){
        video_stream >> capture_frame;
        if (capture_frame.empty()) break; // read frames until end

        //build mats for the first frame
        if (!initialized){
            initializeMats(capture_frame, capture_image, capture_gray,
                           prev_image, prev_gray);
            capture_frame.copyTo(prev_image);
            cvtColor(prev_image, prev_gray, CV_BGR2GRAY);
            initialized = true;
//            LOG(INFO)<<"Initialized";
        }else if(iter % step == 0){
            capture_frame.copyTo(capture_image);
            cvtColor(capture_image, capture_gray, CV_BGR2GRAY);
            calcOpticalFlowFarneback(prev_gray, capture_gray, flow,
                                     0.702, 5, 10, 2, 7, 1.5,
                                     cv::OPTFLOW_FARNEBACK_GAUSSIAN );

            vector<uchar> str_x, str_y, str_img;
            split(flow, flow_split);
            encodeFlowMap(flow_split[0], flow_split[0], str_x, str_y, bound);
            imencode(".jpg", capture_image, str_img);

            output_x.push_back(str_x);
            output_y.push_back(str_y);
            output_img.push_back(str_img);
//            LOG(INFO)<<iter;

            std::swap(prev_gray, capture_gray);
            std::swap(prev_image, capture_image);
        }
    }

}
Ejemplo n.º 6
0
void FrameJPEG::set(cv::Mat &frame)
{
  lock();
  m_pFrame = &frame;
  m_time = getutimestamp();

  // JPEG compression
  std::vector<int> param = std::vector<int>(2);
  param[0] = CV_IMWRITE_JPEG_QUALITY;
  param[1] = m_quality;

  m_buffer.clear();
  imencode(".jpg", frame, m_buffer, param);
  m_time = getutimestamp();
  unlock();

  MJPEGServerManager::checkConnection();

}
Ejemplo n.º 7
0
Image JpegImage::toOpenCCTVImage(const Image& imageInput, const Mat& imageOutput, const string& sResult)
{
    Image imageResutObj;

    //vector<char> resultImageData(imageOutput.cols * imageOutput.rows);
    //vector<char> resultImageData(imageOutput.rows * imageOutput.cols);
    //memcpy(resultImageData.data(), imageOutput.data, resultImageData.size() * sizeof(char));

    vector<uchar> imageOutputJpegData;
    imencode(".jpeg", imageOutput, imageOutputJpegData);

    imageResutObj.setHeight(imageOutput.rows);
    imageResutObj.setWidth(imageOutput.cols);
    imageResutObj.setInputName(imageInput.getInputName());
    imageResutObj.setStreamId(imageInput.getStreamId());
    imageResutObj.setTimestamp(imageInput.getTimestamp());
    vector<char> temp(imageOutputJpegData.begin(), imageOutputJpegData.end());
    imageResutObj.setImageData(temp);
    imageResutObj.setResult(sResult);

    return imageResutObj;
}
Ejemplo n.º 8
0
void MainWindow::save()
{
    if (!_dst.data) {
        QMessageBox::critical(0, "Error", "There is nothing to be saved");
        return;
    }

    QString imageName = QFileDialog::getSaveFileName(this,"Save Image", "", "Image Files (*.jpg)");
    if (imageName == NULL)
        return;

    imwrite(imageName.toAscii().data(), _dst);

    if (isEncryption) {
        AES aes(key);

        int buf_size[2];

        vector<unsigned char> mask_buf, secret_buf;
        unsigned char *temp;

        vector<int> p;
        p.push_back(CV_IMWRITE_JPEG_QUALITY);
        p.push_back(50);
        imencode(".jpg", zone, mask_buf, p);

        for (int i = (((mask_buf.size()+15)>>4)<<4) - mask_buf.size(); i > 0; i--)
            mask_buf.push_back(0);

        for (unsigned int i = 0; i < mask_buf.size(); i+=16) {
            temp = &mask_buf[0]+i;
            aes.Cipher(temp);
        }

        Mat secret;
        dilate(zone, zone, Mat(7,7,CV_8U,Scalar(1)));
        _src.copyTo(secret, zone);
        imencode(".jpg", secret, secret_buf);

        for (int i = (((secret_buf.size()+15)>>4)<<4) - secret_buf.size(); i > 0; i--)
            secret_buf.push_back(0);

        for (unsigned int i = 0; i < secret_buf.size(); i+=16) {
            temp = &secret_buf[0]+i;
            aes.Cipher(temp);
        }

        unsigned char info[16] = "";
        buf_size[0] = mask_buf.size();
        buf_size[1] = secret_buf.size();
        memcpy(info, buf_size, sizeof(int)*2);
        memcpy(info+8, "SEAcret", 7);
        aes.Cipher(info);

        // append encrypted block of image and encryted info to blurred image
        FILE *f = fopen(imageName.toAscii().data(), "ab");
        fwrite(&mask_buf[0], 1, mask_buf.size(), f);
        fwrite(&secret_buf[0], 1, secret_buf.size(), f);
        fwrite(info, 1, 16, f);
        fclose(f);
    }
}
Ejemplo n.º 9
0
void* streamSend(void* arg)
{
    struct sockaddr_in serverAddr, clientAddr, dest;
    //pthread_detach(pthread_self());

    /* make this thread cancellable using pthread_cancel() */
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

    if ((connectSock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
            quit("socket() failed.", 1);
    }

    // Set up server
    dest.sin_family = AF_INET;
    dest.sin_addr.s_addr = inet_addr("140.114.86.199");
    dest.sin_port = htons(9899);
    socklen_t destLen = sizeof(dest);
 /*
    if (bind(listenSock, (sockaddr*)&serverAddr, sizeof(serverAddr)) == -1) {
        quit("bind() failed", 1);
    }

    if (listen(listenSock, 5) == -1) {
        quit("listen() failed.", 1);
    }
*/
    int imgSize = img1.total() * img1.elemSize();
    int bytes=0;
    /* start send images */
    while(1)
    {
        //cout << "-->Waiting for TCP connection on port " << listenPort << " ...\n\n";
    //connect(connectSock, (sockaddr*)&clientAddr, clientAddrLen);
        while(1){
            /* send the grayscaled frame, thread safe */
            if (is_data_ready) {

                pthread_mutex_lock(&mutex1);
                // Compress img1 frame to jpeg...
                vector<uchar>buff;
                vector<int> param = vector<int>(2);
                param[0] = CV_IMWRITE_JPEG_QUALITY;
                param[1] = 10; // default(95) 0 - 100
                imencode(".jpg", img1, buff, param); // encode img1 to jpeg format
                imgSize = (int)buff.size();
                // Get the frame spec = > note that buff: 255 216 (FFD8) and the end is 255 217 (FFD9) is the correct format for a JPEG file
                // Note that: we the imgsize is too large to send out, that is we should seperate the stream to send out
             /*   char len[10];
                sprintf(len, "%8d", imgSize);
                // Send Header
                if ((bytes = send(connectSock, len, 8, 0)) < 0){
                    cerr << "\n--> bytes = " << bytes << endl;
                    quit("\n--> send() failed", 1);
                }
                */
                // Send Data
                if ((bytes = sendto(connectSock, buff.data(), imgSize, 0, (struct sockaddr*)&dest, destLen)) < 0){
                    cerr << "\n--> bytes = " << bytes << endl;
                    quit("\n--> send() failed", 1);
                }

                // Succeed to send image bytes
                is_data_ready = 0;
                pthread_mutex_unlock(&mutex1);
                memset(&clientAddr, 0x0, sizeof(clientAddr));
            }
            usleep(500); //1000 Micro Sec

        }
    }

}
Ejemplo n.º 10
0
		int analyse(const char* filename, const char* outFile){
			Mat img = cv_imread(filename), imgG, imgTmp;
			if(!img.data){
				//cerr << "invalid image" << endl;
				throw "[ghosts] invalid image";
			}
			cvtColor(img, imgG, CV_BGR2GRAY);
			int bCntX = (img.cols - w ) / bSize, bCntY = (img.rows - w) / bSize;
			int bCnt = bCntX * bCntY;
			int bCntR = bCnt;
			int res = 0;
			vector<vector<vector<double> > > bGhosts(bCnt);
			vector<vector<vector<double> > > bInv(bCnt);
			vector<double> bPredict(bCnt);
			for(int i = 0; i < bCnt; i++){
				bGhosts[i].resize(w * w);
				for(int j = 0; j < w * w; j++)
					bGhosts[i][j].resize(101);
			}
			double elaAv = 0.0;
			for(int q = min(qNorm, qMin); q <= qMax; q++){
				if(!(q == qNorm || (q >= qMin && q <= qMax)))
					continue;
				for(int j = 0; j < w * w; j++){
					Rect curRoi = Rect(j % w, j / w, img.cols - j % w, img.rows - j / w);
					vector<int> compParams;
					vector<uchar> buff;
					compParams.push_back(CV_IMWRITE_JPEG_QUALITY);
					compParams.push_back(q);
					imencode(".jpg", imgG(curRoi), buff, compParams); 
					imgTmp = imdecode(buff, 0);
					for(int i = 0; i < bCnt; i++){
						bGhosts[i][j][q] = calcDiff(imgG(curRoi), imgTmp, bCoordX, bCoordY, bCoordX + bSize, bCoordY + bSize);
						if(q == qNorm && j == 1)
							elaAv += bGhosts[i][j][q];
					}
				}
				fprintf(stderr, "\r%d", q);
			}
			elaAv /= bCnt;
			for(int i = 0; i < bCnt; i++){
				vector<double> predictArgsV;
				predictArgsV.push_back(bCoordX);
				predictArgsV.push_back(bCoordY);
				predictArgsV.push_back(elaAv);
				predictArgsV.push_back(bGhosts[i][1][qNorm]);
				bPredict[i] = predictF(imgG, predictArgsV);
				if(bPredict[i] == 1)
					bCntR--;
			}
			for(int i = 0; i < bCnt; i++)
				for(int j = 0; j < w * w; j++)
					bGhosts[i][j] = nrm(bGhosts[i][j]);
			if(1){
				for(int i = 0; i < bCnt; i++){
					bInv[i].resize(101);
					if(bPredict[i] == 1)
						continue;
					vector<double> m1V(101), m2V(101);
					for(int q = qMin; q <= qMax; q++){
						for(int j = 1; j < w * w; j++) // -- j = 1
							m1V[q] += bGhosts[i][j][q];
						m1V[q] /= w * w - 1;
					}
					for(int q = qMin; q <= qMax; q++){
						for(int j = 1; j < w * w; j++) // -- j = 1
							m2V[q] += bGhosts[i][j][q] * bGhosts[i][j][q];
						m2V[q] = sqrt(m2V[q] / (w * w - 1) - m1V[q] * m1V[q]);
					}
					for(int q = qMin; q <= qMax; q++){
						double maxD = -1, minD = 2;
						for(int j = 1; j < w * w; j++){ // -- j = 1
							if(bGhosts[i][j][q] < minD)
								minD = bGhosts[i][j][q];	
							if(bGhosts[i][j][q] > maxD)
								maxD = bGhosts[i][j][q];
						}
						double dlt = max((maxD - minD) / 3.0, 0.01);
						double dlt1 = max((maxD - minD) / 6.0, 0.004);
						if((bGhosts[i][0][q] < minD - dlt || bGhosts[i][0][q] > maxD + dlt)/* && q <= qMax - 3 && q >= qMin + 3*/){
							bInv[i][q].push_back(1);	
						}else
							bInv[i][q].push_back(0);
						if((bGhosts[i][0][q] < minD - dlt1 || bGhosts[i][0][q] > maxD + dlt1)/* && q <= qMax - 3 && q >= qMin + 3*/){
							bInv[i][q].push_back(1);	
						}else
							bInv[i][q].push_back(0);
					}
				}
				vector<double> qMask(101);
				int qMaskCnt = 0;
				for(int q = qMin; q <= qMax; q++){
					for(int i = 0; i < bCnt; i++){
						if(bPredict[i] == 1)
							continue;
						if(bInv[i][q][0] == 1)
							qMask[q]++;
					}
					if(qMask[q] / bCntR > 0.5){
						qMask[q] = 1;
						qMaskCnt++;
					}else
						qMask[q] = 0;
					cerr << qMask[q];
				}
				cerr << endl;
				if(qMaskCnt < 3){
					//cerr << "don't want to analyse" << endl;
					//throw "[ghosts] don't want to analyse";
					return -1;
				}
				for(int i = 0; i < bCnt; i++){
					int clr;
					if(bPredict[i] == 1)
						clr = 0;
					else{
						int tmpI2 = 0;
						for(int q = qMin; q <= qMax; q++)
							if(qMask[q] == 1 && bInv[i][q][1] == 1){
								tmpI2++;
							}
						if((tmpI2 <= 0 && qMaskCnt >= 3) ||
							(tmpI2 <= 1 && qMaskCnt >= 4) ||
							(tmpI2 <= 2 && qMaskCnt >= 5) || 
							(tmpI2 <= 3 && qMaskCnt >= 8))
							tmpI2 = 0;
						clr = (tmpI2 == 0);
					}
					if(clr){
						res++;
						for(int ix = bCoordX; ix < bCoordX + bSize; ix++)
							for(int iy = bCoordY; iy < bCoordY + bSize; iy++)
								img.at<Vec3b>(iy, ix)[2] = 255;
					}
				}
				if(outFile)
					cv_imwrite(outFile, img);
			}
			return res;
		}