/robowaifu/ - DIY Robot Wives

Advancing robotics to a point where anime catgrill meidos in tiny miniskirts are a reality!


New Reply
Name
×
Email
Subject
Message
Files Max 5 files50MB total
Tegaki
Password
[New Reply]


Jschan updated, report in >>>/meta/ if anything is wrong

He is Risen!!  :^)


python_logo.png
[Hide] (81.6KB, 601x203)
c++_logo.png
[Hide] (15.2KB, 1600x1600)
A thread for links, examples, & discussion for software development, primarily intended to focus on Python & C++ . Obviously the endgoal being the crafting of quality code & systems for our robowaifus.
I'll just leave this here for now.

<--->

C++26 Senders/Receivers [1] Async :
Partition Sorting, & OpenCV -based [2] Image Processing Examples
https://accu.org/journals/overload/33/185/teodorescu/

---
1.
https://en.cppreference.com/w/cpp/execution
2.
https://docs.opencv.org/4.11.0/
Last edited by chobitsu
Replies: >>663
>>662
>image-processing related

cv::Mat tr_apply_mask(const cv::Mat& img_main, const cv::Mat& img_mask);
cv::Mat tr_blur(const cv::Mat& src, int size);
cv::Mat tr_to_grayscale(const cv::Mat& src);
cv::Mat tr_adaptthresh(const cv::Mat& img, int block_size, int diff);
cv::Mat tr_reducecolors(const cv::Mat& img, int num_colors) cv::Mat
    tr_oilpainting(const cv::Mat& img, int size, int dyn_ratio);
auto tr_cartoonify(const cv::Mat& src, int blur_size, int num_colors,
                   int block_size, int diff);
auto error_to_exception();
std::vector<std::byte> read_file(const fs::directory_entry& file);
void write_file(const char* filename, const std::vector<unsigned char>& data);
exec::task<int> process_files(const char* in_folder_name,
                              const char* out_folder_name, int blur_size,
                              int num_colors, int block_size, int diff);

int main()
{
  auto everything =
      process_files("data", "out", blur_size, num_colors, block_size, diff);
  auto [processed] = stdexec::sync_wait(std::move(everything)).value();
  printf("Processed images: %d\n", processed);
  return 0;
}

auto tr_cartoonify(const cv::Mat& src, int blur_size, int num_colors,
                   int block_size, int diff)
{
  auto                 sched = exec::get_system_scheduler();
  stdexec::sender auto snd =
      stdexec::when_all(
          stdexec::transfer_just(sched, src) | error_to_exception() |
              stdexec::then([=](const cv::Mat& src) {
                auto blurred = tr_blur(src, blur_size);
                auto gray    = tr_to_grayscale(blurred);
                return tr_adaptthresh(gray, block_size, diff);
              }),
          stdexec::transfer_just(sched, src) | error_to_exception() |
              stdexec::then([=](const cv::Mat& src) {
                return tr_reducecolors(src, num_colors);
              })) |
      stdexec::then([](const cv::Mat& edges, const cv::Mat& reduced_colors) {
        return tr_apply_mask(reduced_colors, edges);
      });
  return snd;
}

auto error_to_exception()
{
  return stdexec::let_error([](auto e) {
    if constexpr (std::same_as<decltype((e)), std::exception_ptr>)
      return stdexec::just_error(e);
    else
      return stdexec::just_error(
          std::make_exception_ptr(std::runtime_error("other error")));
  });
}

exec::task<int> process_files(const char* in_folder_name,
                              const char* out_folder_name, int blur_size,
                              int num_colors, int block_size, int diff)
{
  exec::async_scope        scope;
  exec::static_thread_pool io_pool(1);
  auto                     io_sched  = io_pool.get_scheduler();
  auto                     cpu_sched = exec::get_system_scheduler();
  int                      processed = 0;
  for (const auto& entry : fs::directory_iterator(in_folder_name)) {
    auto extension = entry.path().extension();
    if (! entry.is_regular_file() ||
        (extension != ".jpg") && (extension != ".jpeg"))
      continue;
    auto in_filename = entry.path().string();
    auto out_filename =
        (fs::path(out_folder_name) / entry.path().filename()).string();
    printf(“Processing % s\n”, in_filename.c_str());
    auto file_content =
        co_await (stdexec::schedule(io_sched) |
                  stdexec::then([=] { return read_file(entry); }));
    stdexec::sender auto work = ... scope.spawn(std::move(work));
  }
  co_await scope.on_empty();
  co_return processed;
}

stdexec::sender auto work =
    stdexec::transfer_just(cpu_sched, cv::_InputArray::rawIn(file_content)) |
    error_to_exception() |
    stdexec::then([=](cv::InputArray file_content) -> cv::Mat {
      return cv::imdecode(file_content, cv::IMREAD_COLOR);
    }) |
    stdexec::let_value([=](const cv::Mat& img) {
      return tr_cartoonify(img, blur_size, num_colors, block_size, diff);
    }) |
    stdexec::then([=](const cv::Mat& img) {
      std::vector<unsigned char> out_image_content;
      if (! cv::imencode(extension, img, out_image_content)) {
        throw std::runtime_error("cannot encode image");
      }
      return out_image_content;
    }) |
    stdexec::continues_on(io_sched) |
    stdexec::then([=](const std::vector<unsigned char>& bytes) {
      write_file(out_filename.c_str(), bytes);
    }) |
    stdexec::then([=] { printf("Written %s\n", out_filename.c_str()); }) |
    stdexec::then([&] { processed++; });
Last edited by chobitsu
> (systems-software -related : >>730 )
>via learncpp.com
Here's a straightforward example of testing what C++ standard your compiler is currently using, directly inside your code:

> read_cpp_standard.cpp :
/* minimal build & run:

 g++ read_cpp_standard.cpp -std=c++14 -o read_std && ./read_std

*/

#include <algorithm>
#include <iostream>
#include <map>
#include <string>

using std::cerr;
using std::cout;
using std::find_if;
using std::map;
using std::string;

//------------------------------------------------------------------------------
/** read the compiler's C++ standard version code number
 *
 * @return code number
 */
long get_cpp_standard()
{
  // Visual Studio is non-conforming in support for __cplusplus (unless you set
  // a specific compiler flag, which you probably haven't). in Visual Studio
  // 2015 or newer we can use _MSVC_LANG instead. see
  // https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/
#if defined(_MSVC_LANG)
  return _MSVC_LANG;
#elif defined(_MSC_VER)
  // if we're using an older version of Visual Studio, bail out
  return -1;
#else
  // __cplusplus is the intended way to query the language standard code (as
  // defined by the language standards)
  return __cplusplus;
#endif
}

//------------------------------------------------------------------------------
/** the program's entry point
 *
 * outputs the compiler's C++ language colloquial version number to console
 *
 * @return exit status code (0 -- ie; 'success' -- implicitly)
 */
int main()
{
  auto const standard = get_cpp_standard();

  if (standard == -1) {
    cerr << "ERROR: Unable to determine your compiler's C++ language "
            "standard\n";
    return -1;
  }

  //---

  // note: the C++26 std_code is a placeholder since the exact code won't be
  // determined until the standard is finalized
  map<long, string> const std_codes = {
      {199711L, "Pre-C++11"}, {201103L, "C++11"}, {201402L, "C++14"},
      {201703L, "C++17"},     {202002L, "C++20"}, {202302L, "C++23"},
      {202612L, "C++26"}};

  auto const it =
      find_if(std_codes.cbegin(), std_codes.cend(),
              [standard](auto const& e) { return e.first == standard; });

  if (it != std_codes.cend())  // code found
    cout << "Compiler's C++ standard: " << it->second << '\n';
  else  // code not found
    cout << "Compiler is not using one of the " << std_codes.size()
         << " C++ standards: (" << standard << "L)\n";
}

// see:
// https://www.learncpp.com/cpp-tutorial/what-language-standard-is-my-compiler-using/

// https://en.cppreference.com/w/cpp/feature_test
// https://en.cppreference.com/w/cpp/container/map
// https://en.cppreference.com/w/cpp/algorithm/find

possible output:
Compiler's C++ standard: C++20
Last edited by chobitsu
[New Reply]
4 replies | 2 files
Connecting...
Show Post Actions

Actions:

Captcha:

- news - rules - faq -
jschan 1.7.0