int Converter::makeOriginalPreviewImage(unsigned char* dst_array, int width, int height, bool border) { if (!this->isOpened()) { return -1; } Mat dst_img(height, width, CV_8UC4, dst_array); resize(previewImage, dst_img, dst_img.size()); if (!border) { return 0; } Mat bd_img(n_points_h, n_points_w, CV_8U); for (int s = 0; s < _split_x*_split_y; s++) { for (int n = 0; n < bd_img.size[1]*2 + bd_img.size[0]*2 - 4; n++) { int i, j; if (n < bd_img.size[1]) { i = n; j = 0; } else if (n < bd_img.size[1] + bd_img.size[0] - 1) { i = bd_img.size[1] - 1; j = n - bd_img.size[1] + 1; } else if (n < bd_img.size[1]*2 + bd_img.size[0] - 2) { i = n - bd_img.size[0] - bd_img.size[1] + 2; j = bd_img.size[0] - 1; } else { i = 0; j = n - bd_img.size[0] - bd_img.size[1]*2 + 3; } Point point = calcOriginalPoint(Point(i, j), bd_img.size, dst_img.size, s); int idx = point.x + point.y*dst_img.size[1]; dst_img.data[idx*4+0] = 255; dst_img.data[idx*4+1] = 0; dst_img.data[idx*4+2] = 0; } } return 0; }
int Converter::makeConvertedPreviewImage(unsigned char* dst_array, int width, int height) { if (!this->isOpened()) { return -1; } Mat dst_img(height, width, CV_8UC4, dst_array); convertImage(previewImage, dst_img); return 0; }
int main(int argc, char **argv) { if( argc != 3 ) { std::cerr << "Usage: " << argv[0] << "<InputImage> <NumberOfClusters>" << std::endl; return EXIT_FAILURE; } int NumberOfClusters = atoi( argv[2] ); // (1)load a specified file as a 3-channel color image cv::Mat src_img = cv::imread( argv[1] ); if(!src_img.data) { return EXIT_FAILURE; } // (2)reshape the image to be a 1 column matrix cv::Mat points; src_img.convertTo(points, CV_32FC3); points = points.reshape(3, src_img.rows*src_img.cols); // (3)run k-means clustering algorithm to segment pixels in RGB color space cv::Mat_<int> clusters(points.size(), CV_32SC1); cv::Mat centers; cv::kmeans(points, NumberOfClusters, clusters, cv::TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0), 1, cv::KMEANS_PP_CENTERS, centers); // (4)make a each centroid represent all pixels in the cluster cv::Mat dst_img(src_img.size(), src_img.type()); cv::MatIterator_<cv::Vec3f> itf = centers.begin<cv::Vec3f>(); cv::MatIterator_<cv::Vec3b> itd = dst_img.begin<cv::Vec3b>(), itd_end = dst_img.end<cv::Vec3b>(); for(int i=0; itd != itd_end; ++itd, ++i) { cv::Vec3f color = itf[clusters(1,i)]; (*itd)[0] = cv::saturate_cast<uchar>(color[0]); (*itd)[1] = cv::saturate_cast<uchar>(color[1]); (*itd)[2] = cv::saturate_cast<uchar>(color[2]); } // (5)show source and destination image, and quit when any key pressed cv::namedWindow("src_img", CV_WINDOW_AUTOSIZE); imshow("src_img", src_img); cv::namedWindow("dst_img", CV_WINDOW_AUTOSIZE); imshow("dst_img", dst_img); cv::waitKey(0); cv::imwrite( "kmeans_output.png", dst_img ); return 0; }
bool corpus_gen::generate(const char* pCmd_line) { static const command_line_params::param_desc param_desc_array[] = { { "corpus_gen", 0, false }, { "in", 1, true }, { "deep", 0, false }, { "blockpercentage", 1, false }, { "width", 1, false }, { "height", 1, false }, { "alpha", 0, false }, }; command_line_params params; if (!params.parse(pCmd_line, CRNLIB_ARRAY_SIZE(param_desc_array), param_desc_array, true)) return false; if (!params.has_key("in")) { console::error("Must specify one or more input files using the /in option!"); return false; } uint num_dst_blocks_x = params.get_value_as_int("width", 0, 4096, 128, 4096); num_dst_blocks_x = (num_dst_blocks_x + 3) / 4; uint num_dst_blocks_y = params.get_value_as_int("height", 0, 4096, 128, 4096); num_dst_blocks_y = (num_dst_blocks_y + 3) / 4; const uint total_dst_blocks = num_dst_blocks_x * num_dst_blocks_y; image_u8 dst_img(num_dst_blocks_x * 4, num_dst_blocks_y * 4); uint next_dst_block = 0; uint total_dst_images = 0; random rm; block_hash_map block_hash; block_hash.reserve(total_dst_blocks); uint total_images_loaded = 0; uint total_blocks_written = 0; command_line_params::param_map_const_iterator it = params.begin(); for ( ; it != params.end(); ++it) { if (it->first != "in") continue; if (it->second.m_values.empty()) { console::error("Must follow /in parameter with a filename!\n"); return false; } for (uint in_value_index = 0; in_value_index < it->second.m_values.size(); in_value_index++) { const dynamic_string& filespec = it->second.m_values[in_value_index]; find_files file_finder; if (!file_finder.find(filespec.get_ptr(), find_files::cFlagAllowFiles | (params.has_key("deep") ? find_files::cFlagRecursive : 0))) { console::warning("Failed finding files: %s", filespec.get_ptr()); continue; } if (file_finder.get_files().empty()) { console::warning("No files found: %s", filespec.get_ptr()); return false; } const find_files::file_desc_vec& files = file_finder.get_files(); for (uint file_index = 0; file_index < files.size(); file_index++) { const find_files::file_desc& file_desc = files[file_index]; console::printf("Loading image: %s", file_desc.m_fullname.get_ptr()); image_u8 img; if (!image_utils::read_from_file(img, file_desc.m_fullname.get_ptr(), 0)) { console::warning("Failed loading image file: %s", file_desc.m_fullname.get_ptr()); continue; } if (!params.has_key("alpha")) { for (uint y = 0; y < img.get_height(); y++) for (uint x = 0; x < img.get_width(); x++) img(x, y).a = 255; } total_images_loaded++; uint width = img.get_width(); uint height = img.get_height(); uint num_blocks_x = (width + 3) / 4; uint num_blocks_y = (height + 3) / 4; uint total_blocks = num_blocks_x * num_blocks_y; float percentage = params.get_value_as_float("blockpercentage", 0, .1f, .001f, 1.0f); uint total_rand_blocks = math::maximum<uint>(1U, (uint)(total_blocks * percentage)); crnlib::vector<uint> remaining_blocks(total_blocks); for (uint i = 0; i < total_blocks; i++) remaining_blocks[i] = i; uint num_blocks_remaining = total_rand_blocks; while (num_blocks_remaining) { if (remaining_blocks.empty()) break; uint rand_block_index = rm.irand(0, remaining_blocks.size()); uint block_index = remaining_blocks[rand_block_index]; remaining_blocks.erase_unordered(rand_block_index); uint block_y = block_index / num_blocks_x; uint block_x = block_index % num_blocks_x; block b; for (uint y = 0; y < 4; y++) { for (uint x = 0; x < 4; x++) { b.m_c[x+y*4] = img.get_clamped(block_x*4+x, block_y*4+y); } } if (!block_hash.insert(b).second) continue; if (block_hash.size() == total_dst_blocks) { block_hash.clear(); block_hash.reserve(total_dst_blocks); } uint dst_block_x = next_dst_block % num_dst_blocks_x; uint dst_block_y = next_dst_block / num_dst_blocks_x; for (uint y = 0; y < 4; y++) { for (uint x = 0; x < 4; x++) { dst_img(dst_block_x * 4 + x, dst_block_y * 4 + y) = b.m_c[x + y*4]; } } next_dst_block++; if (total_dst_blocks == next_dst_block) { sort_blocks(dst_img); dynamic_string dst_filename(cVarArg, "test_%u.tga", total_dst_images); console::printf("Writing image: %s", dst_filename.get_ptr()); image_utils::write_to_file(dst_filename.get_ptr(), dst_img, 0); dst_img.set_all(color_quad_u8::make_black()); next_dst_block = 0; total_dst_images++; } total_blocks_written++; num_blocks_remaining--; } } // file_index } // in_value_index } if (next_dst_block) { sort_blocks(dst_img); dynamic_string dst_filename(cVarArg, "test_%u.tga", total_dst_images); console::printf("Writing image: %s", dst_filename.get_ptr()); image_utils::write_to_file(dst_filename.get_ptr(), dst_img, 0); next_dst_block = 0; total_dst_images++; } console::printf("Found %u input images, %u output images, %u total blocks", total_images_loaded, total_dst_images, total_blocks_written); return true; }