int UMat::checkVector(int _elemChannels, int _depth, bool _requireContinuous) const { return (depth() == _depth || _depth <= 0) && (isContinuous() || !_requireContinuous) && ((dims == 2 && (((rows == 1 || cols == 1) && channels() == _elemChannels) || (cols == _elemChannels && channels() == 1))) || (dims == 3 && channels() == 1 && size.p[2] == _elemChannels && (size.p[0] == 1 || size.p[1] == 1) && (isContinuous() || step.p[1] == step.p[2]*size.p[2]))) ? (int)(total()*channels()/_elemChannels) : -1; }
void Parameter::setValue(float val, int chan) { std::cout << "Setting value of " << chan << " to " << val << std::endl; if (isBoolean()) { if (val > 0.0f) values.set(chan, true); else values.set(chan, false); } else if (isContinuous()) { if (val < (float) possibleValues[0]) { values.set(chan, possibleValues[0]); } else if (val > (float) possibleValues[1]) { values.set(chan, possibleValues[1]); } else { values.set(chan, val); } } else { //int index = (int) val; //if (index >= 0 && index < possibleValues.size()) //{ values.set(chan, val); //} } }
UMat UMat::reshape(int new_cn, int new_rows) const { int cn = channels(); UMat hdr = *this; if( dims > 2 && new_rows == 0 && new_cn != 0 && size[dims-1]*cn % new_cn == 0 ) { hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((new_cn-1) << CV_CN_SHIFT); hdr.step[dims-1] = CV_ELEM_SIZE(hdr.flags); hdr.size[dims-1] = hdr.size[dims-1]*cn / new_cn; return hdr; } CV_Assert( dims <= 2 ); if( new_cn == 0 ) new_cn = cn; int total_width = cols * cn; if( (new_cn > total_width || total_width % new_cn != 0) && new_rows == 0 ) new_rows = rows * total_width / new_cn; if( new_rows != 0 && new_rows != rows ) { int total_size = total_width * rows; if( !isContinuous() ) CV_Error( CV_BadStep, "The matrix is not continuous, thus its number of rows can not be changed" ); if( (unsigned)new_rows > (unsigned)total_size ) CV_Error( CV_StsOutOfRange, "Bad new number of rows" ); total_width = total_size / new_rows; if( total_width * new_rows != total_size ) CV_Error( CV_StsBadArg, "The total number of matrix elements " "is not divisible by the new number of rows" ); hdr.rows = new_rows; hdr.step[0] = total_width * elemSize1(); } int new_width = total_width / new_cn; if( new_width * new_cn != total_width ) CV_Error( CV_BadNumChannels, "The total width is not divisible by the new number of channels" ); hdr.cols = new_width; hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((new_cn-1) << CV_CN_SHIFT); hdr.step[1] = CV_ELEM_SIZE(hdr.flags); return hdr; }
UMat UMat::reshape(int _cn, int _newndims, const int* _newsz) const { if(_newndims == dims) { if(_newsz == 0) return reshape(_cn); if(_newndims == 2) return reshape(_cn, _newsz[0]); } if (isContinuous()) { CV_Assert(_cn >= 0 && _newndims > 0 && _newndims <= CV_MAX_DIM && _newsz); if (_cn == 0) _cn = this->channels(); else CV_Assert(_cn <= CV_CN_MAX); size_t total_elem1_ref = this->total() * this->channels(); size_t total_elem1 = _cn; AutoBuffer<int, 4> newsz_buf( (size_t)_newndims ); for (int i = 0; i < _newndims; i++) { CV_Assert(_newsz[i] >= 0); if (_newsz[i] > 0) newsz_buf[i] = _newsz[i]; else if (i < dims) newsz_buf[i] = this->size[i]; else CV_Error(CV_StsOutOfRange, "Copy dimension (which has zero size) is not present in source matrix"); total_elem1 *= (size_t)newsz_buf[i]; } if (total_elem1 != total_elem1_ref) CV_Error(CV_StsUnmatchedSizes, "Requested and source matrices have different count of elements"); UMat hdr = *this; hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((_cn-1) << CV_CN_SHIFT); setSize(hdr, _newndims, (int*)newsz_buf, NULL, true); return hdr; } CV_Error(CV_StsNotImplemented, "Reshaping of n-dimensional non-continuous matrices is not supported yet"); // TBD return UMat(); }
Matrix * Matrix::alphaBeta(double alpha, double beta) { if (isContinuous()) { int end = rows * cols * channels(); uchar *data = ptr<uchar>(0); for (int i = 0; i < end; ++i) { *data = cv::saturate_cast<uchar>(alpha * (*data) + beta); data++; } } else { std::cerr << "Make alpha beta for non continous Mats!" << std::endl; std::exit(EXIT_FAILURE); } return this; }
Mat& Mat::operator = (const Scalar& s) { Size sz = size(); uchar* dst = data; sz.width *= (int)elemSize(); if( isContinuous() ) { sz.width *= sz.height; sz.height = 1; } if( s[0] == 0 && s[1] == 0 && s[2] == 0 && s[3] == 0 ) { for( ; sz.height--; dst += step ) memset( dst, 0, sz.width ); } else { int t = type(), esz1 = (int)elemSize1(); double scalar[12]; scalarToRawData(s, scalar, t, 12); int copy_len = 12*esz1; uchar* dst_limit = dst + sz.width; if( sz.height-- ) { while( dst + copy_len <= dst_limit ) { memcpy( dst, scalar, copy_len ); dst += copy_len; } memcpy( dst, scalar, dst_limit - dst ); } if( sz.height ) { dst = dst_limit - sz.width + step; for( ; sz.height--; dst += step ) memcpy( dst, data, sz.width ); } } return *this; }
cv::Mat loadThumbnails(const std::string& path) { std::vector<fs::path> files; for (fs::directory_iterator it{ fs::path(path) }, end; it != end; ++it) { if (it->path().extension() == ".png" || it->path().extension() == ".jpg") { files.push_back(it->path()); } } auto frameCount = files.size(); auto w = 16; auto h = 16; auto n = frameCount * (w*h); std::cout << "Allocating memory... " << n * 3 * sizeof(float) / 1024 / 1024 << " MB\n"; cv::Mat Z(n, 3, CV_32F); auto Zframe = 0; std::cout << "Reading frames...\n"; auto frameIdx = 0; for (auto f : files) { std::cout << " " << frameIdx << "/" << frameCount << "\n"; auto frame = cv::imread(f.string()); cv::resize(frame, frame, cv::Size(w, h)); assert(frame.isContinuous()); for (int i = 0; i < frame.size().height; ++i) { const cv::Vec3b* row = frame.ptr<cv::Vec3b>(i); for (int j = 0; j < frame.size().width; ++j) { cv::Vec3b v; convert(row[j], v, CV_BGR2Lab); Z.at<float>(Zframe*w*h + i*w + j, 0) = v[0]; Z.at<float>(Zframe*w*h + i*w + j, 1) = v[1]; Z.at<float>(Zframe*w*h + i*w + j, 2) = v[2]; } } ++frameIdx; ++Zframe; } return Z; }
/* dst = src */ void Mat::copyTo( Mat& dst ) const { if( data == dst.data ) return; dst.create( rows, cols, type() ); Size sz = size(); const uchar* sptr = data; uchar* dptr = dst.data; sz.width *= (int)elemSize(); if( isContinuous() && dst.isContinuous() ) { sz.width *= sz.height; sz.height = 1; } for( ; sz.height--; sptr += step, dptr += dst.step ) memcpy( dptr, sptr, sz.width ); }
spn::ByteBuff Surface::extractAsContinuous(uint32_t dstFmt) const { auto& myformat = getFormat(); if(dstFmt == 0) dstFmt = myformat.format; auto lk = lock(); int w = width(), h = height(); // ピクセルデータが隙間なく詰まっていて、なおかつフォーマットも同じならそのままメモリをコピー if(isContinuous() && dstFmt==myformat.format) { auto* src = reinterpret_cast<uint8_t*>(lk.getBits()); return spn::ByteBuff(src, src + w*h*myformat.BytesPerPixel); } auto upFmt = MakeUPFormat(dstFmt); size_t dstSize = w * h * upFmt->BytesPerPixel; spn::ByteBuff dst(dstSize); SDLEC(Trap, SDL_ConvertPixels, w,h, myformat.format, lk.getBits(), lk.getPitch(), dstFmt, &dst[0], w*upFmt->BytesPerPixel); return dst; }