/*void CFishEyeCorrect::ImageCorrect(const Mat& orgImg,Mat& dstImg1,Mat& dstImg2,Mat& dstImg3) { dstImg1 = Mat::zeros(Size(w/2,h/2),CV_8UC3); dstImg2 = Mat::zeros(Size(w/2,h/2),CV_8UC3); dstImg3 = Mat::zeros(Size(w/2,h/2),CV_8UC3); #pragma omp parallel for for (int i =0;i < dstImg1.rows -1; ++i) { int *data1 = ImgIndex1.ptr<int>(i*2); int *data2 = ImgIndex2.ptr<int>(i*2); int *data3 = ImgIndex3.ptr<int>(i*2); for (int j = 0; j < dstImg1.cols-1;++j) { int u1 = data1[j*4]; int v1 = data1[j*4 +1]; int u2 = data2[j*4]; int v2 = data2[j*4 +1]; int u3 = data3[j*4]; int v3 = data3[j*4 +1]; if (u1 < orgImg.rows-1 && u1 > -1 && v1 < orgImg.cols-1 && v1 > -1 ) { *(dstImg1.data + dstImg1.step[0]*i + dstImg1.step[1]*j ) = *(orgImg.data + orgImg.step[0]*u1 + orgImg.step[1]*v1 ); *(dstImg1.data + dstImg1.step[0]*i + dstImg1.step[1]*j + dstImg1.elemSize1()) =*(orgImg.data + orgImg.step[0]*u1 + orgImg.step[1]*v1 + orgImg.elemSize1()); *(dstImg1.data + dstImg1.step[0]*i + dstImg1.step[1]*j + dstImg1.elemSize1() * 2) =*(orgImg.data + orgImg.step[0]*u1 + orgImg.step[1]*v1 + orgImg.elemSize1() * 2); } if (u2 < orgImg.rows-1 && u2 > -1 && v2 < orgImg.cols-1 && v2 > -1 ) { *(dstImg2.data + dstImg2.step[0]*i + dstImg2.step[1]*j ) = *(orgImg.data + orgImg.step[0]*u2 + orgImg.step[1]*v2 ); *(dstImg2.data + dstImg2.step[0]*i + dstImg2.step[1]*j + dstImg2.elemSize1()) =*(orgImg.data + orgImg.step[0]*u2 + orgImg.step[1]*v2 + orgImg.elemSize1()); *(dstImg2.data + dstImg2.step[0]*i + dstImg2.step[1]*j + dstImg2.elemSize1() * 2) =*(orgImg.data + orgImg.step[0]*u2 + orgImg.step[1]*v2 + orgImg.elemSize1() * 2); } if (u3 < orgImg.rows-1 && u3 > -1 && v3 < orgImg.cols-1 && v3 > -1 ) { *(dstImg3.data + dstImg3.step[0]*i + dstImg3.step[1]*j ) = *(orgImg.data + orgImg.step[0]*u3 + orgImg.step[1]*v3 ); *(dstImg3.data + dstImg3.step[0]*i + dstImg3.step[1]*j + dstImg3.elemSize1()) =*(orgImg.data + orgImg.step[0]*u3 + orgImg.step[1]*v3 + orgImg.elemSize1()); *(dstImg3.data + dstImg3.step[0]*i + dstImg3.step[1]*j + dstImg3.elemSize1() * 2) =*(orgImg.data + orgImg.step[0]*u3 + orgImg.step[1]*v3 + orgImg.elemSize1() * 2); } } } } */ void ImageCorrect(const Mat& orgImg,Mat& dstImg,Mat& ImgIndex ) { dstImg = Mat::zeros(Size(w/2,h/2),CV_8UC3); //#pragma omp parallel for for (int i =0;i < dstImg1.rows -1; ++i) { int *data = ImgIndex.ptr<int>(i*2); for (int j = 0; j < dstImg1.cols-1;++j) { int u = data[j*4]; int v = data[j*4 +1]; if (u < orgImg.rows-1 && u > -1 && v < orgImg.cols-1 && v > -1 ) { *(dstImg.data + dstImg.step[0]*i + dstImg.step[1]*j ) = *(orgImg.data + orgImg.step[0]*u + orgImg.step[1]*v ); *(dstImg.data + dstImg.step[0]*i + dstImg.step[1]*j + dstImg.elemSize1()) =*(orgImg.data + orgImg.step[0]*u + orgImg.step[1]*v + orgImg.elemSize1()); *(dstImg.data + dstImg.step[0]*i + dstImg.step[1]*j + dstImg.elemSize1() * 2) =*(orgImg.data + orgImg.step[0]*u + orgImg.step[1]*v + orgImg.elemSize1() * 2); } } } }
void cv::ocl::oclMat::upload(const Mat &m) { if (!Context::getContext()->supportsFeature(FEATURE_CL_DOUBLE) && m.depth() == CV_64F) { CV_Error(Error::OpenCLDoubleNotSupported, "Selected device doesn't support double"); return; } CV_DbgAssert(!m.empty()); Size wholeSize; Point ofs; m.locateROI(wholeSize, ofs); create(wholeSize, m.type()); if (m.channels() == 3) { int pitch = wholeSize.width * 3 * m.elemSize1(); int tail_padding = m.elemSize1() * 3072; int err; cl_mem temp = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, (pitch * wholeSize.height + tail_padding - 1) / tail_padding * tail_padding, 0, &err); openCLVerifyCall(err); openCLMemcpy2D(clCxt, temp, pitch, m.datastart, m.step, wholeSize.width * m.elemSize(), wholeSize.height, clMemcpyHostToDevice, 3); convert_C3C4(temp, *this); openCLSafeCall(clReleaseMemObject(temp)); } else openCLMemcpy2D(clCxt, data, step, m.datastart, m.step, wholeSize.width * elemSize(), wholeSize.height, clMemcpyHostToDevice); rows = m.rows; cols = m.cols; offset = ofs.y * step + ofs.x * elemSize(); }
void cv::ocl::oclMat::upload(const Mat &m) { CV_DbgAssert(!m.empty()); Size wholeSize; Point ofs; m.locateROI(wholeSize, ofs); // int type = m.type(); // if(m.oclchannels() == 3) //{ // type = CV_MAKETYPE(m.depth(), 4); //} create(wholeSize, m.type()); if(m.channels() == 3) { int pitch = wholeSize.width * 3 * m.elemSize1(); int tail_padding = m.elemSize1() * 3072; int err; cl_mem temp = clCreateBuffer((cl_context)clCxt->oclContext(), CL_MEM_READ_WRITE, (pitch * wholeSize.height + tail_padding - 1) / tail_padding * tail_padding, 0, &err); openCLVerifyCall(err); openCLMemcpy2D(clCxt, temp, pitch, m.datastart, m.step, wholeSize.width * m.elemSize(), wholeSize.height, clMemcpyHostToDevice, 3); convert_C3C4(temp, *this); //int* cputemp=new int[wholeSize.height*wholeSize.width * 3]; //int* cpudata=new int[this->step*this->wholerows/sizeof(int)]; //openCLSafeCall(clEnqueueReadBuffer(clCxt->impl->clCmdQueue, temp, CL_TRUE, // 0, wholeSize.height*wholeSize.width * 3* sizeof(int), cputemp, 0, NULL, NULL)); //openCLSafeCall(clEnqueueReadBuffer(clCxt->impl->clCmdQueue, (cl_mem)data, CL_TRUE, // 0, this->step*this->wholerows, cpudata, 0, NULL, NULL)); //for(int i=0;i<wholeSize.height;i++) //{ // int *a = cputemp+i*wholeSize.width * 3,*b = cpudata + i*this->step/sizeof(int); // for(int j=0;j<wholeSize.width;j++) // { // if((a[3*j] != b[4*j])||(a[3*j+1] != b[4*j+1])||(a[3*j+2] != b[4*j+2])) // printf("rows=%d,cols=%d,cputtemp=%d,%d,%d;cpudata=%d,%d,%d\n", // i,j,a[3*j],a[3*j+1],a[3*j+2],b[4*j],b[4*j+1],b[4*j+2]); // } //} //delete []cputemp; //delete []cpudata; openCLSafeCall(clReleaseMemObject(temp)); } else { openCLMemcpy2D(clCxt, data, step, m.datastart, m.step, wholeSize.width * elemSize(), wholeSize.height, clMemcpyHostToDevice); } rows = m.rows; cols = m.cols; offset = ofs.y * step + ofs.x * elemSize(); //download_channels = m.channels(); }
template<typename _Tp> void resizeMatChannelType(Mat& src_mat, Mat& dest_mat, double val) { CV_Assert(!src_mat.empty() && !dest_mat.empty()); CV_Assert(src_mat.cols == dest_mat.cols && src_mat.rows == dest_mat.rows); int src_channel = src_mat.channels(); int dest_channel = dest_mat.channels(); int diff_channel = dest_channel - src_channel; int byte_size = src_mat.elemSize1(); int src_block = src_mat.elemSize(); int dest_block = dest_mat.elemSize(); int mat_size = src_mat.rows * src_mat.cols; int copy_channel = (diff_channel < 0) ? dest_channel : src_channel; int copy_byte = copy_channel * byte_size; int type = src_mat.depth(); unsigned char* src_data = src_mat.data; unsigned char* dest_data = dest_mat.data; unsigned char* dest_ptr; int i,j; for(i=0; i<mat_size; i++){ memcpy(dest_data+i*dest_block, src_data+i*src_block, copy_byte); for(j=0;j<diff_channel;j++){ dest_ptr = dest_data + i*dest_block + copy_byte + j*byte_size; *((_Tp*)dest_ptr) = (_Tp)val; } } }
void wrapCVMat(Mat& cvMat, image_t& img) { img.height = cvMat.rows; img.width = cvMat.cols; img.depth = cvMat.channels(); img.pitch = cvMat.cols*cvMat.elemSize(); img.itemSize = cvMat.elemSize1(); img.data = cvMat.ptr(); }
void cv::ocl::oclMat::upload(const Mat &m) { CV_DbgAssert(!m.empty()); Size wholeSize; Point ofs; m.locateROI(wholeSize, ofs); if(m.channels() == 3) { create(wholeSize, m.type()); int pitch = wholeSize.width * 3 * m.elemSize1(); int tail_padding = m.elemSize1() * 3072; int err; cl_mem temp; if(gDeviceMemType!=DEVICE_MEM_UHP && gDeviceMemType!=DEVICE_MEM_CHP){ temp = clCreateBuffer((cl_context)clCxt->oclContext(), CL_MEM_READ_WRITE, (pitch * wholeSize.height + tail_padding - 1) / tail_padding * tail_padding, 0, &err); openCLVerifyCall(err); openCLMemcpy2D(clCxt, temp, pitch, m.datastart, m.step, wholeSize.width * m.elemSize(), wholeSize.height, clMemcpyHostToDevice, 3); } else{ temp = clCreateBuffer((cl_context)clCxt->oclContext(), CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, (pitch * wholeSize.height + tail_padding - 1) / tail_padding * tail_padding, m.datastart, &err); openCLVerifyCall(err); } convert_C3C4(temp, *this); openCLSafeCall(clReleaseMemObject(temp)); } else { // try to use host ptr createEx(wholeSize, m.type(), gDeviceMemRW, gDeviceMemType, m.datastart); if(gDeviceMemType!=DEVICE_MEM_UHP && gDeviceMemType!=DEVICE_MEM_CHP) openCLMemcpy2D(clCxt, data, step, m.datastart, m.step, wholeSize.width * elemSize(), wholeSize.height, clMemcpyHostToDevice); } rows = m.rows; cols = m.cols; offset = ofs.y * step + ofs.x * elemSize(); }
void cv::split(const Mat& src, Mat* mv) { int k, depth = src.depth(), cn = src.channels(); if( cn == 1 ) { src.copyTo(mv[0]); return; } SplitFunc func = splitTab[depth]; CV_Assert( func != 0 ); int esz = (int)src.elemSize(), esz1 = (int)src.elemSize1(); int blocksize0 = (BLOCK_SIZE + esz-1)/esz; AutoBuffer<uchar> _buf((cn+1)*(sizeof(Mat*) + sizeof(uchar*)) + 16); const Mat** arrays = (const Mat**)(uchar*)_buf; uchar** ptrs = (uchar**)alignPtr(arrays + cn + 1, 16); arrays[0] = &src; for( k = 0; k < cn; k++ ) { mv[k].create(src.dims, src.size, depth); arrays[k+1] = &mv[k]; } NAryMatIterator it(arrays, ptrs, cn+1); int total = (int)it.size, blocksize = cn <= 4 ? total : std::min(total, blocksize0); for( size_t i = 0; i < it.nplanes; i++, ++it ) { for( int j = 0; j < total; j += blocksize ) { int bsz = std::min(total - j, blocksize); func( ptrs[0], &ptrs[1], bsz, cn ); if( j + blocksize < total ) { ptrs[0] += bsz*esz; for( k = 0; k < cn; k++ ) ptrs[k+1] += bsz*esz1; } } } }
void split(const Mat& src, Mat* mv) { static SplitFunc tab[] = { splitC2_<uchar>, splitC2_<ushort>, splitC2_<int>, 0, splitC2_<int64>, splitC3_<uchar>, splitC3_<ushort>, splitC3_<int>, 0, splitC3_<int64>, splitC4_<uchar>, splitC4_<ushort>, splitC4_<int>, 0, splitC4_<int64> }; int i, depth = src.depth(), cn = src.channels(); if( cn == 1 ) { src.copyTo(mv[0]); return; } for( i = 0; i < cn; i++ ) mv[i].create(src.dims, src.size, depth); if( cn <= 4 ) { SplitFunc func = tab[(cn-2)*5 + (src.elemSize1()>>1)]; CV_Assert( func != 0 ); if( src.dims > 2 ) { const Mat* arrays[5]; Mat planes[5]; arrays[0] = &src; for( i = 0; i < cn; i++ ) arrays[i+1] = &mv[i]; NAryMatIterator it(arrays, planes, cn+1); for( int i = 0; i < it.nplanes; i++, ++it ) func( it.planes[0], &it.planes[1] ); } else func( src, mv ); }
void FisheyeImgCorrect::ImageCorrect(Mat& orgImg,Mat& dstImg1,Mat& dstImg2,Mat& dstImg3) { //FishCalculate(orgImg); dstImg1 = Mat::zeros(Size(w/2,h/2),CV_8UC3); dstImg2 = Mat::zeros(Size(w/2,h/2),CV_8UC3); dstImg3 = Mat::zeros(Size(w/2,h/2),CV_8UC3); for (int i =0;i < dstImg1.rows -1; ++i) { int *data1 = ImgIndex1.ptr<int>(i*2); int *data2 = ImgIndex2.ptr<int>(i*2); int *data3 = ImgIndex3.ptr<int>(i*2); for (int j = 0; j < dstImg1.cols-1;++j) { int u1 = data1[j*4]; int v1 = data1[j*4 +1]; int u2 = data2[j*4]; int v2 = data2[j*4 +1]; int u3 = data3[j*4]; int v3 = data3[j*4 +1]; if (u1 < orgImg.rows-1 && u1 > -1 && v1 < orgImg.cols-1 && v1 > -1 ) { //dstImg1.at<Vec3b>(i,j) = orgImg.at<Vec3b>(u1,v1); *(dstImg1.data + dstImg1.step[0]*i + dstImg1.step[1]*j ) = *(orgImg.data + orgImg.step[0]*u1 + orgImg.step[1]*v1 ); *(dstImg1.data + dstImg1.step[0]*i + dstImg1.step[1]*j + dstImg1.elemSize1()) =*(orgImg.data + orgImg.step[0]*u1 + orgImg.step[1]*v1 + orgImg.elemSize1()); *(dstImg1.data + dstImg1.step[0]*i + dstImg1.step[1]*j + dstImg1.elemSize1() * 2) =*(orgImg.data + orgImg.step[0]*u1 + orgImg.step[1]*v1 + orgImg.elemSize1() * 2); } if (u2 < orgImg.rows-1 && u2 > -1 && v2 < orgImg.cols-1 && v2 > -1 ) { //dstImg2.at<Vec3b>(i,j) = orgImg.at<Vec3b>(u2, v2); *(dstImg2.data + dstImg2.step[0]*i + dstImg2.step[1]*j ) = *(orgImg.data + orgImg.step[0]*u2 + orgImg.step[1]*v2 ); *(dstImg2.data + dstImg2.step[0]*i + dstImg2.step[1]*j + dstImg2.elemSize1()) =*(orgImg.data + orgImg.step[0]*u2 + orgImg.step[1]*v2 + orgImg.elemSize1()); *(dstImg2.data + dstImg2.step[0]*i + dstImg2.step[1]*j + dstImg2.elemSize1() * 2) =*(orgImg.data + orgImg.step[0]*u2 + orgImg.step[1]*v2 + orgImg.elemSize1() * 2); } if (u3 < orgImg.rows-1 && u3 > -1 && v3 < orgImg.cols-1 && v3 > -1 ) { //dstImg3.at<Vec3b>(i,j) = orgImg.at<Vec3b>(u3,v3); *(dstImg3.data + dstImg3.step[0]*i + dstImg3.step[1]*j ) = *(orgImg.data + orgImg.step[0]*u3 + orgImg.step[1]*v3 ); *(dstImg3.data + dstImg3.step[0]*i + dstImg3.step[1]*j + dstImg3.elemSize1()) =*(orgImg.data + orgImg.step[0]*u3 + orgImg.step[1]*v3 + orgImg.elemSize1()); *(dstImg3.data + dstImg3.step[0]*i + dstImg3.step[1]*j + dstImg3.elemSize1() * 2) =*(orgImg.data + orgImg.step[0]*u3 + orgImg.step[1]*v3 + orgImg.elemSize1() * 2); } } } }
bool TiffDecoder::readData( Mat& img ) { bool result = false; bool color = img.channels() > 1; uchar* data = img.data; int step = (int)img.step; if( img.depth() != CV_8U && img.depth() != CV_16U && img.depth() != CV_32F && img.depth() != CV_64F ) return false; if( m_tif && m_width && m_height ) { TIFF* tif = (TIFF*)m_tif; int tile_width0 = m_width, tile_height0 = 0; int x, y, i; int is_tiled = TIFFIsTiled(tif); int photometric; TIFFGetField( tif, TIFFTAG_PHOTOMETRIC, &photometric ); int bpp = 8, ncn = photometric > 1 ? 3 : 1; TIFFGetField( tif, TIFFTAG_BITSPERSAMPLE, &bpp ); TIFFGetField( tif, TIFFTAG_SAMPLESPERPIXEL, &ncn ); const int bitsPerByte = 8; int dst_bpp = (int)(img.elemSize1() * bitsPerByte); if(dst_bpp == 8) { char errmsg[1024]; if(!TIFFRGBAImageOK( tif, errmsg )) { close(); return false; } } if( (!is_tiled) || (is_tiled && TIFFGetField( tif, TIFFTAG_TILEWIDTH, &tile_width0 ) && TIFFGetField( tif, TIFFTAG_TILELENGTH, &tile_height0 ))) { if(!is_tiled) TIFFGetField( tif, TIFFTAG_ROWSPERSTRIP, &tile_height0 ); if( tile_width0 <= 0 ) tile_width0 = m_width; if( tile_height0 <= 0 ) tile_height0 = m_height; AutoBuffer<uchar> _buffer(tile_height0*tile_width0*8); uchar* buffer = _buffer; ushort* buffer16 = (ushort*)buffer; float* buffer32 = (float*)buffer; double* buffer64 = (double*)buffer; int tileidx = 0; for( y = 0; y < m_height; y += tile_height0, data += step*tile_height0 ) { int tile_height = tile_height0; if( y + tile_height > m_height ) tile_height = m_height - y; for( x = 0; x < m_width; x += tile_width0, tileidx++ ) { int tile_width = tile_width0, ok; if( x + tile_width > m_width ) tile_width = m_width - x; switch(dst_bpp) { case 8: { if( !is_tiled ) ok = TIFFReadRGBAStrip( tif, y, (uint32*)buffer ); else ok = TIFFReadRGBATile( tif, x, y, (uint32*)buffer ); if( !ok ) { close(); return false; } for( i = 0; i < tile_height; i++ ) if( color ) icvCvt_BGRA2BGR_8u_C4C3R( buffer + i*tile_width*4, 0, data + x*3 + step*(tile_height - i - 1), 0, cvSize(tile_width,1), 2 ); else icvCvt_BGRA2Gray_8u_C4C1R( buffer + i*tile_width*4, 0, data + x + step*(tile_height - i - 1), 0, cvSize(tile_width,1), 2 ); break; } case 16: { if( !is_tiled ) ok = (int)TIFFReadEncodedStrip( tif, tileidx, (uint32*)buffer, (tsize_t)-1 ) >= 0; else ok = (int)TIFFReadEncodedTile( tif, tileidx, (uint32*)buffer, (tsize_t)-1 ) >= 0; if( !ok ) { close(); return false; } for( i = 0; i < tile_height; i++ ) { if( color ) { if( ncn == 1 ) { icvCvt_Gray2BGR_16u_C1C3R(buffer16 + i*tile_width*ncn, 0, (ushort*)(data + step*i) + x*3, 0, cvSize(tile_width,1) ); } else if( ncn == 3 ) { icvCvt_RGB2BGR_16u_C3R(buffer16 + i*tile_width*ncn, 0, (ushort*)(data + step*i) + x*3, 0, cvSize(tile_width,1) ); } else { icvCvt_BGRA2BGR_16u_C4C3R(buffer16 + i*tile_width*ncn, 0, (ushort*)(data + step*i) + x*3, 0, cvSize(tile_width,1), 2 ); } } else { if( ncn == 1 ) { memcpy((ushort*)(data + step*i)+x, buffer16 + i*tile_width*ncn, tile_width*sizeof(buffer16[0])); } else { icvCvt_BGRA2Gray_16u_CnC1R(buffer16 + i*tile_width*ncn, 0, (ushort*)(data + step*i) + x, 0, cvSize(tile_width,1), ncn, 2 ); } } } break; } case 32: case 64: { if( !is_tiled ) ok = (int)TIFFReadEncodedStrip( tif, tileidx, buffer, (tsize_t)-1 ) >= 0; else ok = (int)TIFFReadEncodedTile( tif, tileidx, buffer, (tsize_t)-1 ) >= 0; if( !ok || ncn != 1 ) { close(); return false; } for( i = 0; i < tile_height; i++ ) { if(dst_bpp == 32) { memcpy((float*)(data + step*i)+x, buffer32 + i*tile_width*ncn, tile_width*sizeof(buffer32[0])); } else { memcpy((double*)(data + step*i)+x, buffer64 + i*tile_width*ncn, tile_width*sizeof(buffer64[0])); } } break; } default: { close(); return false; } } } } result = true; } } close(); return result; }
bool PxMEncoder::write( const Mat& img, const std::vector<int>& params ) { bool isBinary = true; int width = img.cols, height = img.rows; int _channels = img.channels(), depth = (int)img.elemSize1()*8; int channels = _channels > 1 ? 3 : 1; int fileStep = width*(int)img.elemSize(); int x, y; for( size_t i = 0; i < params.size(); i += 2 ) if( params[i] == CV_IMWRITE_PXM_BINARY ) isBinary = params[i+1] != 0; WLByteStream strm; if( m_buf ) { if( !strm.open(*m_buf) ) return false; int t = CV_MAKETYPE(img.depth(), channels); m_buf->reserve( alignSize(256 + (isBinary ? fileStep*height : ((t == CV_8UC1 ? 4 : t == CV_8UC3 ? 4*3+2 : t == CV_16UC1 ? 6 : 6*3+2)*width+1)*height), 256)); } else if( !strm.open(m_filename) ) return false; int lineLength; int bufferSize = 128; // buffer that should fit a header if( isBinary ) lineLength = width * (int)img.elemSize(); else lineLength = (6 * channels + (channels > 1 ? 2 : 0)) * width + 32; if( bufferSize < lineLength ) bufferSize = lineLength; AutoBuffer<char> _buffer(bufferSize); char* buffer = _buffer; // write header; sprintf( buffer, "P%c\n%d %d\n%d\n", '2' + (channels > 1 ? 1 : 0) + (isBinary ? 3 : 0), width, height, (1 << depth) - 1 ); strm.putBytes( buffer, (int)strlen(buffer) ); for( y = 0; y < height; y++ ) { uchar* data = img.data + img.step*y; if( isBinary ) { if( _channels == 3 ) { if( depth == 8 ) icvCvt_BGR2RGB_8u_C3R( (uchar*)data, 0, (uchar*)buffer, 0, cvSize(width,1) ); else icvCvt_BGR2RGB_16u_C3R( (ushort*)data, 0, (ushort*)buffer, 0, cvSize(width,1) ); } // swap endianness if necessary if( depth == 16 && !isBigEndian() ) { if( _channels == 1 ) memcpy( buffer, data, fileStep ); for( x = 0; x < width*channels*2; x += 2 ) { uchar v = buffer[x]; buffer[x] = buffer[x + 1]; buffer[x + 1] = v; } } strm.putBytes( (channels > 1 || depth > 8) ? buffer : (char*)data, fileStep ); } else { char* ptr = buffer; if( channels > 1 ) { if( depth == 8 ) { for( x = 0; x < width*channels; x += channels ) { sprintf( ptr, "% 4d", data[x + 2] ); ptr += 4; sprintf( ptr, "% 4d", data[x + 1] ); ptr += 4; sprintf( ptr, "% 4d", data[x] ); ptr += 4; *ptr++ = ' '; *ptr++ = ' '; } } else { for( x = 0; x < width*channels; x += channels ) { sprintf( ptr, "% 6d", ((ushort *)data)[x + 2] ); ptr += 6; sprintf( ptr, "% 6d", ((ushort *)data)[x + 1] ); ptr += 6; sprintf( ptr, "% 6d", ((ushort *)data)[x] ); ptr += 6; *ptr++ = ' '; *ptr++ = ' '; } } } else { if( depth == 8 ) { for( x = 0; x < width; x++ ) { sprintf( ptr, "% 4d", data[x] ); ptr += 4; } } else { for( x = 0; x < width; x++ ) { sprintf( ptr, "% 6d", ((ushort *)data)[x] ); ptr += 6; } } } *ptr++ = '\n'; strm.putBytes( buffer, (int)(ptr - buffer) ); } } strm.close(); return true; }
static void convertFromCCS( const Mat& _src0, const Mat& _src1, Mat& _dst, int flags ) { if( _dst.rows > 1 && (_dst.cols > 1 || (flags & DFT_ROWS)) ) { int i, count = _dst.rows, len = _dst.cols; bool is2d = (flags & DFT_ROWS) == 0; Mat src0row, src1row, dstrow; for( i = 0; i < count; i++ ) { int j = !is2d || i == 0 ? i : count - i; src0row = _src0.row(i); src1row = _src1.row(j); dstrow = _dst.row(i); convertFromCCS( src0row, src1row, dstrow, 0 ); } if( is2d ) { src0row = _src0.col(0); dstrow = _dst.col(0); convertFromCCS( src0row, src0row, dstrow, 0 ); if( (len & 1) == 0 ) { src0row = _src0.col(_src0.cols - 1); dstrow = _dst.col(len/2); convertFromCCS( src0row, src0row, dstrow, 0 ); } } } else { int i, n = _dst.cols + _dst.rows - 1, n2 = (n+1) >> 1; int cn = _src0.channels(); int srcstep = cn, dststep = 1; if( !_dst.isContinuous() ) dststep = (int)(_dst.step/_dst.elemSize()); if( !_src0.isContinuous() ) srcstep = (int)(_src0.step/_src0.elemSize1()); if( _dst.depth() == CV_32F ) { Complexf* dst = _dst.ptr<Complexf>(); const float* src0 = _src0.ptr<float>(); const float* src1 = _src1.ptr<float>(); int delta0, delta1; dst->re = src0[0]; dst->im = 0; if( (n & 1) == 0 ) { dst[n2*dststep].re = src0[(cn == 1 ? n-1 : n2)*srcstep]; dst[n2*dststep].im = 0; } delta0 = srcstep; delta1 = delta0 + (cn == 1 ? srcstep : 1); if( cn == 1 ) srcstep *= 2; for( i = 1; i < n2; i++, delta0 += srcstep, delta1 += srcstep ) { float t0 = src0[delta0]; float t1 = src0[delta1]; dst[i*dststep].re = t0; dst[i*dststep].im = t1; t0 = src1[delta0]; t1 = -src1[delta1]; dst[(n-i)*dststep].re = t0; dst[(n-i)*dststep].im = t1; } } else { Complexd* dst = _dst.ptr<Complexd>(); const double* src0 = _src0.ptr<double>(); const double* src1 = _src1.ptr<double>(); int delta0, delta1; dst->re = src0[0]; dst->im = 0; if( (n & 1) == 0 ) { dst[n2*dststep].re = src0[(cn == 1 ? n-1 : n2)*srcstep]; dst[n2*dststep].im = 0; } delta0 = srcstep; delta1 = delta0 + (cn == 1 ? srcstep : 1); if( cn == 1 ) srcstep *= 2; for( i = 1; i < n2; i++, delta0 += srcstep, delta1 += srcstep ) { double t0 = src0[delta0]; double t1 = src0[delta1]; dst[i*dststep].re = t0; dst[i*dststep].im = t1; t0 = src1[delta0]; t1 = -src1[delta1]; dst[(n-i)*dststep].re = t0; dst[(n-i)*dststep].im = t1; } } } }
void cv::merge(const Mat* mv, size_t n, OutputArray _dst) { CV_Assert( mv && n > 0 ); int depth = mv[0].depth(); bool allch1 = true; int k, cn = 0; size_t i; for( i = 0; i < n; i++ ) { CV_Assert(mv[i].size == mv[0].size && mv[i].depth() == depth); allch1 = allch1 && mv[i].channels() == 1; cn += mv[i].channels(); } CV_Assert( 0 < cn && cn <= CV_CN_MAX ); _dst.create(mv[0].dims, mv[0].size, CV_MAKETYPE(depth, cn)); Mat dst = _dst.getMat(); if( n == 1 ) { mv[0].copyTo(dst); return; } if( !allch1 ) { AutoBuffer<int> pairs(cn*2); int j, ni=0; for( i = 0, j = 0; i < n; i++, j += ni ) { ni = mv[i].channels(); for( k = 0; k < ni; k++ ) { pairs[(j+k)*2] = j + k; pairs[(j+k)*2+1] = j + k; } } mixChannels( mv, n, &dst, 1, &pairs[0], cn ); return; } size_t esz = dst.elemSize(), esz1 = dst.elemSize1(); int blocksize0 = (int)((BLOCK_SIZE + esz-1)/esz); AutoBuffer<uchar> _buf((cn+1)*(sizeof(Mat*) + sizeof(uchar*)) + 16); const Mat** arrays = (const Mat**)(uchar*)_buf; uchar** ptrs = (uchar**)alignPtr(arrays + cn + 1, 16); arrays[0] = &dst; for( k = 0; k < cn; k++ ) arrays[k+1] = &mv[k]; NAryMatIterator it(arrays, ptrs, cn+1); int total = (int)it.size, blocksize = cn <= 4 ? total : std::min(total, blocksize0); MergeFunc func = mergeTab[depth]; for( i = 0; i < it.nplanes; i++, ++it ) { for( int j = 0; j < total; j += blocksize ) { int bsz = std::min(total - j, blocksize); func( (const uchar**)&ptrs[1], ptrs[0], bsz, cn ); if( j + blocksize < total ) { ptrs[0] += bsz*esz; for( int k = 0; k < cn; k++ ) ptrs[k+1] += bsz*esz1; } } } }
bool cnn::getImg(Mat matImg) { if(matImg.empty()) { return false; } else { int ch = matImg.channels(); int width = matImg.cols; int height = matImg.rows; input = new double[ch*width*height]; if(ch==3) { for(int t=0; t<ch; t++) { for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { input[t*width*height+row*width+col] = ((static_cast<double>(*(matImg.data + matImg.step[0] * row + matImg.step[1] * col + matImg.elemSize1() * (2-t))) / 255)-0.5)*2; } } } } else if(ch==1) { for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { input[row*width+col] = ((static_cast<double>(*(matImg.data + matImg.step[0] * row + matImg.step[1] * col)) / 255)-0.5)*2; } } } return true; } }
// opencv void resize_opt_INTER_LINEAR2(Mat &src, Mat &dst,Size dsize, double fx, double fy) { int depth = src.depth(), cn = src.channels(); size_t srcstep = src.step / src.elemSize1(); //5760 Size ssize = src.size(), dsize_ = dst.size(); Size ssize1 = ssize; Size dsize1_ = dsize_; double scale_x = 1./fx, scale_y = 1./fy; int iscale_x = saturate_cast<int>(scale_x); int iscale_y = saturate_cast<int>(scale_y); int area = iscale_x*iscale_y; //4 AutoBuffer<int> _ofs(area + dsize_.width*cn); int* ofs = _ofs; int* xofs = ofs + area; float scale = 1.f/(area); int dwidth1 = (ssize.width/scale_x)*cn; dsize1_.width *= cn; ssize1.width *= cn; int dy, dx, k = 0; int sx, sy; for( sy = 0, k = 0; sy < iscale_y; sy++ ) for( sx = 0; sx < iscale_x; sx++ ) ofs[k++] = (int)(sy*srcstep + sx*cn); for( dx = 0; dx < dsize_.width; dx++ ) { int j = dx * cn; sx = iscale_x * j; for( k = 0; k < cn; k++ ) xofs[j + k] = sx + k; } Range range(0, dst.rows); for( dy = range.start; dy < range.end; dy++ ) { T* D = (T*)(dst.data + dst.step*dy); int sy0 = dy*scale_y; int w = sy0 + scale_y <= ssize1.height ? dwidth1 : 0; if( sy0 >= ssize1.height ) { for( dx = 0; dx < dsize1_.width; dx++ ) D[dx] = 0; continue; } #if 0 { const T* nextS = (const T*)((const uchar*)src.data + src.step); int dx = 0; if (cn == 1) for( ; dx < w; ++dx ) { int index = dx*2; D[dx] = (T)((S[index] + S[index+1] + nextS[index] + nextS[index+1] + 2) >> 2); } else if (cn == 3) for( ; dx < w; dx += 3 ) { int index = dx*2; D[dx] = (T)((S[index] + S[index+3] + nextS[index] + nextS[index+3] + 2) >> 2); D[dx+1] = (T)((S[index+1] + S[index+4] + nextS[index+1] + nextS[index+4] + 2) >> 2); D[dx+2] = (T)((S[index+2] + S[index+5] + nextS[index+2] + nextS[index+5] + 2) >> 2); } else { assert(cn == 4); for( ; dx < w; dx += 4 ) { int index = dx*2; D[dx] = (T)((S[index] + S[index+4] + nextS[index] + nextS[index+4] + 2) >> 2); D[dx+1] = (T)((S[index+1] + S[index+5] + nextS[index+1] + nextS[index+5] + 2) >> 2); D[dx+2] = (T)((S[index+2] + S[index+6] + nextS[index+2] + nextS[index+6] + 2) >> 2); D[dx+3] = (T)((S[index+3] + S[index+7] + nextS[index+3] + nextS[index+7] + 2) >> 2); } } }
bool PAMEncoder::write( const Mat& img, const std::vector<int>& params ) { WLByteStream strm; int width = img.cols, height = img.rows; int stride = width*(int)img.elemSize(); const uchar* data = img.ptr(); const struct pam_format *fmt = NULL; int x, y, tmp, bufsize = 256; /* parse save file type */ for( size_t i = 0; i < params.size(); i += 2 ) if( params[i] == CV_IMWRITE_PAM_TUPLETYPE ) { if ( params[i+1] > CV_IMWRITE_PAM_FORMAT_NULL && params[i+1] < (int) PAM_FORMATS_NO) fmt = &formats[params[i+1]]; } if( m_buf ) { if( !strm.open(*m_buf) ) return false; m_buf->reserve( alignSize(256 + stride*height, 256)); } else if( !strm.open(m_filename) ) return false; tmp = width * (int)img.elemSize(); if (bufsize < tmp) bufsize = tmp; AutoBuffer<char> _buffer(bufsize); char* buffer = _buffer.data(); /* write header */ tmp = 0; tmp += sprintf( buffer, "P7\n"); tmp += sprintf( buffer + tmp, "WIDTH %d\n", width); tmp += sprintf( buffer + tmp, "HEIGHT %d\n", height); tmp += sprintf( buffer + tmp, "DEPTH %d\n", img.channels()); tmp += sprintf( buffer + tmp, "MAXVAL %d\n", (1 << img.elemSize1()*8) - 1); if (fmt) tmp += sprintf( buffer + tmp, "TUPLTYPE %s\n", fmt->name ); sprintf( buffer + tmp, "ENDHDR\n" ); strm.putBytes( buffer, (int)strlen(buffer) ); /* write data */ if (img.depth() == CV_8U) strm.putBytes( data, stride*height ); else if (img.depth() == CV_16U) { /* fix endianness */ if (!isBigEndian()) { for( y = 0; y < height; y++ ) { memcpy( buffer, img.ptr(y), stride ); for( x = 0; x < stride; x += 2 ) { uchar v = buffer[x]; buffer[x] = buffer[x + 1]; buffer[x + 1] = v; } strm.putBytes( buffer, stride ); } } else strm.putBytes( data, stride*height ); } else CV_Error(Error::StsInternal, ""); strm.close(); return true; }