cv::Mat3b OpenCVUtil::tile_image_patches(const std::vector<cv::Mat3b>& images, int tileCols, int tileRows, int patchWidth, int patchHeight, int paddingSize) { // Scale and pad the input images to make the tiles. std::vector<cv::Mat3b> tiles; for(size_t i = 0, size = images.size(); i < size; ++i) { cv::Mat3b scaledImage(patchHeight, patchWidth); cv::resize(images[i], scaledImage, scaledImage.size(), 0.0, 0.0, CV_INTER_NN); tiles.push_back(pad_image(scaledImage, paddingSize)); } // Make a blank output image of the right size to which the tiles can be copied. const int tileHeight = patchHeight + 2 * paddingSize; const int tileWidth = patchWidth + 2 * paddingSize; cv::Mat3b tiledImage = cv::Mat3b::zeros(tileHeight * tileRows, tileWidth * tileCols); // Copy the tiles across to the output image. for(int i = 0, tileCount = tileCols * tileRows; i < tileCount; ++i) { int x = (i % tileCols) * tileWidth; int y = (i / tileCols) * tileHeight; cv::Rect roi(x, y, tileWidth, tileHeight); tiles[i].copyTo(tiledImage(roi)); } return tiledImage; }
static int write_horizontal_file(state *s) { int32_t i,j,location; unsigned char c; for (i = 0 ; i < s->image_width ; i++) { for (j = 0 ; j < s->image_height ; ++j) { if (s->direction) location = (s->image_width * (j+1)) - (i+1); else location = s->image_height * (s->image_width - i -1) + j; if (location < s->input_length) { fseeko(s->in_handle,location,SEEK_SET); c = fgetc(s->in_handle); } else { // We pad the image with black when necessary c = padding(); } write_byte(s,c); } pad_image(s,s->image_height); } return FALSE; }
int main(int argc, char* argv[]) { uint32_t pad_mask; int ret = EXIT_FAILURE; int err; int i; progname = basename(argv[0]); if (argc < 2) { fprintf(stderr, "Usage: %s file [pad0] [pad1] [padN]\n", progname); goto out; } pad_mask = 0; for (i = 2; i < argc; i++) pad_mask |= strtoul(argv[i], NULL, 0) * 1024; if (pad_mask == 0) pad_mask = (4 * 1024) | (8 * 1024) | (64 * 1024) | (128 * 1024); err = pad_image(argv[1], pad_mask); if (err) goto out; ret = EXIT_SUCCESS; out: return ret; }
static int write_vertical_file(state *s) { int32_t i,j, bytes_read = 0; off_t location; int32_t offset = (s->image_width * s->image_height) - s->image_width; unsigned char c; #ifdef DEBUG print_status ("offset %"PRId32" length %"PRId32" width %"PRId32"\n", offset, s->input_length, s->image_width); #endif for (i = 0 ; i < s->image_height ; ++i) { for (j = 0 ; j < s->image_width ; ++j) { if (s->direction) { location = offset - (s->image_width * i) + j; if (location < s->input_length) { #ifdef DEBUG print_status ("bytes read %06"PRId32" seeking to %06"PRId32, bytes_read, location); #endif if (fseeko(s->in_handle, location, SEEK_SET)) { return TRUE; } c = fgetc(s->in_handle); } else { #ifdef DEBUG print_status ("padding"); #endif // We pad the image with black when necessary c = padding(); } } else { if (bytes_read < s->input_length) c = fgetc(s->in_handle); else c = padding(); } ++bytes_read; write_byte(s, c); } if (s->direction) pad_image(s,s->image_width); } return FALSE; }
int main(int argc, char* argv[]) { uint32_t pad_mask; int ret = EXIT_FAILURE; int err; int i; progname = basename(argv[0]); if (argc < 2) { fprintf(stderr, "Usage: %s file [-x <xtra offset>] [pad0] [pad1] [padN]\n", progname); goto out; } pad_mask = 0; for (i = 2; i < argc; i++) { if (i == 2 && strcmp(argv[i], "-x") == 0) { i++; xtra_offset = strtoul(argv[i], NULL, 0); fprintf(stderr, "assuming %u bytes offset\n", xtra_offset); continue; } pad_mask |= strtoul(argv[i], NULL, 0) * 1024; } if (pad_mask == 0) pad_mask = (4 * 1024) | (8 * 1024) | (64 * 1024) | (128 * 1024); err = pad_image(argv[1], pad_mask); if (err) goto out; ret = EXIT_SUCCESS; out: return ret; }
sk_sp<SkSpecialImage> SkImageFilter::applyCropRect(const Context& ctx, SkSpecialImage* src, SkIPoint* srcOffset, SkIRect* bounds) const { const SkIRect srcBounds = SkIRect::MakeXYWH(srcOffset->x(), srcOffset->y(), src->width(), src->height()); SkIRect dstBounds = this->onFilterNodeBounds(srcBounds, ctx.ctm(), kForward_MapDirection); fCropRect.applyTo(dstBounds, ctx.ctm(), this->affectsTransparentBlack(), bounds); if (!bounds->intersect(ctx.clipBounds())) { return nullptr; } if (srcBounds.contains(*bounds)) { return sk_sp<SkSpecialImage>(SkRef(src)); } else { sk_sp<SkSpecialImage> img(pad_image(src, bounds->width(), bounds->height(), srcOffset->x() - bounds->x(), srcOffset->y() - bounds->y())); *srcOffset = SkIPoint::Make(bounds->x(), bounds->y()); return img; } }
typename ImageFactory<T>::view_type* rotate(const T &src, double angle, typename T::value_type bgcolor, int order) { if (order < 1 || order > 3) { throw std::range_error("Order must be between 1 and 3"); } if (src.nrows()<2 && src.ncols()<2) return simple_image_copy(src); // Adjust angle to a positive double between 0-360 while(angle<0.0) angle+=360; while(angle>=360.0) angle-=360; // some angle ranges flip width and height // as VIGRA requires source and destination to be of the same // size, it cannot handle a reduce in one image dimension. // Hence we must rotate by 90 degrees, if necessary bool rot90done = false; typename ImageFactory<T>::view_type* prep4vigra = (typename ImageFactory<T>::view_type*) &src; if ((45 < angle && angle < 135) || (225 < angle && angle < 315)) { typename ImageFactory<T>::data_type* prep4vigra_data = new typename ImageFactory<T>::data_type(Size(src.height(),src.width())); prep4vigra = new typename ImageFactory<T>::view_type(*prep4vigra_data); size_t ymax = src.nrows() - 1; for (size_t y = 0; y < src.nrows(); ++y) { for (size_t x = 0; x < src.ncols(); ++x) { prep4vigra->set(Point(ymax-y,x), src.get(Point(x,y))); } } rot90done = true; // recompute rotation angle, because partial rotation already done angle -= 90.0; if (angle < 0.0) angle +=360; } double rad = (angle / 180.0) * M_PI; // new width/height depending on angle size_t new_width, new_height; if ((0 <= angle && angle <= 90) || (180 <= angle && angle <= 270)) { new_width = size_t(0.5+abs(cos(rad) * (double)prep4vigra->width() + sin(rad) * (double)prep4vigra->height())); new_height = size_t(0.5+abs(sin(rad) * (double)prep4vigra->width() + cos(rad) * (double)prep4vigra->height())); } else { new_width = size_t(0.5+abs(cos(rad) * (double)prep4vigra->width() - sin(rad) * (double)prep4vigra->height())); new_height = size_t(0.5+abs(sin(rad) * (double)prep4vigra->width() - cos(rad) * (double)prep4vigra->height())); } size_t pad_width = 0; if (new_width > prep4vigra->width()) pad_width = (new_width - prep4vigra->width()) / 2 + 2; size_t pad_height = 0; if (new_height > prep4vigra->height()) pad_height = (new_height - prep4vigra->height()) / 2 + 2; typename ImageFactory<T>::view_type* tmp = pad_image(*prep4vigra, pad_height, pad_width, pad_height, pad_width, bgcolor); typename ImageFactory<T>::data_type* dest_data = new typename ImageFactory<T>::data_type(tmp->size()); typename ImageFactory<T>::view_type* dest = new typename ImageFactory<T>::view_type(*dest_data); try { fill(*dest, bgcolor); if (order == 1) { vigra::SplineImageView<1, typename T::value_type> spline(src_image_range(*tmp)); vigra::rotateImage(spline, dest_image(*dest), -angle); } else if (order == 2) { vigra::SplineImageView<2, typename T::value_type> spline(src_image_range(*tmp)); vigra::rotateImage(spline, dest_image(*dest), -angle); } else if (order == 3) { vigra::SplineImageView<3, typename T::value_type> spline(src_image_range(*tmp)); vigra::rotateImage(spline, dest_image(*dest), -angle); } } catch (std::exception e) { delete tmp->data(); delete tmp; delete dest; delete dest_data; if (rot90done) { delete prep4vigra->data(); delete prep4vigra; } throw; } if (rot90done) { delete prep4vigra->data(); delete prep4vigra; } delete tmp->data(); delete tmp; return dest; }
cl_int ConvolutionLayerSpatial<float>::convolve( const vector<Blob<float>*>& bottom, const vector<Blob<float>*>& top, int_tp index, int_tp numImages, kernelConfig* config) { viennacl::ocl::context &ctx = viennacl::ocl::get_context(this->device_->id()); viennacl::ocl::program & program = ctx.get_program(config->kernelName); viennacl::ocl::kernel &kernel = program.get_kernel(config->kernelName); cl_int err = 0; if (config->kernelType != 2) { for (int_tp n = 0; n < numImages; ++n) { for (int_tp g = 0; g < group_; ++g) { bias_offset_ = M_ * g; int_tp image_offset = n * this->bottom_dim_ + width_ * height_ * (channels_ / group_) * g; int_tp output_image_offset = n * this->top_dim_ + output_w_ * output_h_ * M_ * g; cl_uint argIdx = 0; int_tp kernel_offset = kernel_h_ * kernel_w_ * (channels_ / group_) * M_ * g; // Copy image if (pad_w_ > 0 || pad_h_ > 0) { pad_image(bottom, top, image_offset, config, numImages); image_offset = 0; kernel.arg(argIdx++, WrapHandle((cl_mem) col_data, &ctx)); } else { kernel.arg(argIdx++, WrapHandle((cl_mem) bottom_data, &ctx)); } kernel.arg(argIdx++, image_offset); kernel.arg(argIdx++, WrapHandle((cl_mem) weight, &ctx)); kernel.arg(argIdx++, kernel_offset); kernel.arg(argIdx++, WrapHandle((cl_mem) bias_, &ctx)); kernel.arg(argIdx++, bias_offset_); kernel.arg(argIdx++, WrapHandle((cl_mem) top_data, &ctx)); kernel.arg(argIdx++, output_image_offset); kernel.arg(argIdx++, (uint16_t)padded_width_); kernel.arg(argIdx++, (uint16_t)padded_height_); kernel.arg(argIdx++, (uint16_t)output_w_); kernel.arg(argIdx++, (uint16_t)output_h_); if (config->use_null_local) { err = clEnqueueNDRangeKernel(ctx.get_queue().handle().get(), kernel.handle().get(), 3, NULL, config->global_work_size, NULL, 0, NULL, NULL); } else { err = clEnqueueNDRangeKernel(ctx.get_queue().handle().get(), kernel.handle().get(), 3, NULL, config->global_work_size, config->local_work_size, 0, NULL, NULL); } if (err != CL_SUCCESS) return err; viennacl::backend::finish(); } } } else { swizzleWeights(bottom, top, 16); size_t total_bottom_size = bottom_dim_ * numImages; size_t total_kernel_size = kernel_h_ * kernel_w_ * channels_ * M_; size_t total_bias_size = M_ * group_; size_t total_top_size = top_dim_ * numImages; for (int_tp g = 0; g < group_; ++g) { bias_offset_ = M_ * g; int_tp image_offset = width_ * height_ * (channels_ / group_) * g; int_tp output_image_offset = output_w_ * output_h_ * M_ * g; cl_uint argIdx = 0; int_tp kernel_offset = kernel_h_ * kernel_w_ * (channels_ / group_) * M_ * g; // Copy image cl_mem input_image; if (pad_w_ > 0 || pad_h_ > 0) { pad_image(bottom, top, image_offset, config, numImages); image_offset = 0; input_image = (cl_mem) col_data; } else { input_image = (cl_mem) bottom_data; } setBufferKernelArg(bottom, top, &kernel, argIdx++, &ctx, input_image, image_offset, total_bottom_size - image_offset, true, false); setBufferKernelArg(bottom, top, &kernel, argIdx++, &ctx, (cl_mem) swizzled_weights, kernel_offset, total_kernel_size - kernel_offset, true, true); setBufferKernelArg(bottom, top, &kernel, argIdx++, &ctx, (cl_mem) bias_, bias_offset_, total_bias_size - bias_offset_, true, true); setBufferKernelArg(bottom, top, &kernel, argIdx++, &ctx, (cl_mem) top_data, output_image_offset, total_top_size - output_image_offset, false, false); kernel.arg(argIdx++, (uint16_t)padded_width_); kernel.arg(argIdx++, (uint16_t)padded_height_); kernel.arg(argIdx++, (uint16_t)output_w_); kernel.arg(argIdx++, (uint16_t)output_h_); err = clEnqueueNDRangeKernel(ctx.get_queue().handle().get(), kernel.handle().get(), 3, NULL, config->global_work_size, config->local_work_size, 0, NULL, NULL); if (err != CL_SUCCESS) return err; viennacl::backend::finish(); } if (group_ > 1) { viennacl::backend::finish(); cleanTmpSubBuffers(bottom, top); } } return err; }