<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re:different behavior when type casting from float to uchar in Intel® oneAPI DPC++/C++ Compiler</title>
    <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/different-behavior-when-type-casting-from-float-to-uchar/m-p/1598107#M3752</link>
    <description>&lt;P&gt;Can you tell us how to build your code? Which libraries of opencv need to be linked?&lt;/P&gt;&lt;BR /&gt;</description>
    <pubDate>Thu, 16 May 2024 03:38:42 GMT</pubDate>
    <dc:creator>cw_intel</dc:creator>
    <dc:date>2024-05-16T03:38:42Z</dc:date>
    <item>
      <title>different behavior when type casting from float to uchar</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/different-behavior-when-type-casting-from-float-to-uchar/m-p/1593419#M3692</link>
      <description>&lt;P&gt;I am using dpc++ compiler integrated with microsoft visual studio, I wrote a small app to apply a filter to an image (gray scale), the app will compute the new value of the pixel on the device and store it on new filtered_image_buffer, while computing, the value is float and then it will be converted to uchar but the behavior is different when I use cpu vs gpu selector, on gpu the filter is applied correctly and the result image is fine, but in cpu the image is in chaos.&lt;BR /&gt;cpu that I am using: Intel(R) Core(TM) i7-8665U&lt;BR /&gt;gpu: Intel(R) UHD Graphics 620&lt;BR /&gt;here the results for gpu vs cpu (filter to sharpen the image is used)&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="NabeelKh_0-1714378957863.png" style="width: 400px;"&gt;&lt;img src="https://community.intel.com/t5/image/serverpage/image-id/54236i757E16E03CE19EA8/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400&amp;amp;whitelist-exif-data=Orientation%2CResolution%2COriginalDefaultFinalSize%2CCopyright" role="button" title="NabeelKh_0-1714378957863.png" alt="NabeelKh_0-1714378957863.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="NabeelKh_1-1714378967550.png" style="width: 400px;"&gt;&lt;img src="https://community.intel.com/t5/image/serverpage/image-id/54237i012D6930F6623980/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400&amp;amp;whitelist-exif-data=Orientation%2CResolution%2COriginalDefaultFinalSize%2CCopyright" role="button" title="NabeelKh_1-1714378967550.png" alt="NabeelKh_1-1714378967550.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;code:&lt;BR /&gt;```&lt;/P&gt;&lt;P&gt;#include &amp;lt;sycl/sycl.hpp&amp;gt;&lt;BR /&gt;#include &amp;lt;iostream&amp;gt;&lt;BR /&gt;#include &amp;lt;opencv2/opencv.hpp&amp;gt;&lt;/P&gt;&lt;P&gt;using namespace sycl;&lt;/P&gt;&lt;P&gt;int main(int argc, char* argv[]) {&lt;BR /&gt;cv::Mat image = cv::imread("oneapi1.png", cv::IMREAD_GRAYSCALE);&lt;BR /&gt;if (image.empty()) {&lt;BR /&gt;std::cout &amp;lt;&amp;lt; "Could not open or find the image" &amp;lt;&amp;lt; std::endl;&lt;BR /&gt;return -1;&lt;BR /&gt;}&lt;BR /&gt;std::vector&amp;lt;uchar&amp;gt; imageData;&lt;BR /&gt;imageData.assign(image.data, image.data + image.total());&lt;BR /&gt;buffer &amp;lt;uchar, 1&amp;gt; imageBuffer(imageData.data(), range&amp;lt;1&amp;gt;(image.total()));&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;cv::Mat filteredImage = image.clone();&lt;BR /&gt;std::vector&amp;lt;uchar&amp;gt; filteredImageData;&lt;BR /&gt;filteredImageData.assign(filteredImage.data, filteredImage.data + filteredImage.total() * filteredImage.channels());&lt;BR /&gt;cpu_selector selector;&lt;BR /&gt;queue q{ selector };&lt;BR /&gt;auto height = image.rows;&lt;BR /&gt;auto width = image.cols;&lt;BR /&gt;const int filterHeight = 5;&lt;BR /&gt;const int filterWidth = 5;&lt;BR /&gt;float filter[filterHeight][filterWidth] = {&lt;BR /&gt;{-1, -1, -1, -1, -1,},&lt;BR /&gt;{-1, 1.5, 1.5, 1.5, -1},&lt;BR /&gt;{-1, 1.5, 5, 1.5, -1},&lt;BR /&gt;{-1, 1.5, 1.5, 1.5, -1},&lt;BR /&gt;{-1, -1, -1, -1, -1}&lt;BR /&gt;};&lt;BR /&gt;buffer &amp;lt;float, 2&amp;gt; filterBuf(range&amp;lt;2&amp;gt;(filterHeight, filterWidth));&lt;BR /&gt;{&lt;BR /&gt;auto filterBufAcc = filterBuf.get_access&amp;lt;access::mode::read_write&amp;gt;();&lt;BR /&gt;for (int i = 0; i &amp;lt; filterHeight; i++) {&lt;BR /&gt;for (int j = 0; j &amp;lt; filterWidth; j++) {&lt;BR /&gt;filterBufAcc[i][j] = filter[i][j];&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;buffer &amp;lt;uchar, 1&amp;gt; filteredImageBuffer(filteredImageData.data(), range&amp;lt;1&amp;gt;(filteredImage.total() * filteredImage.channels()));&lt;BR /&gt;q.submit([&amp;amp;](handler&amp;amp; h) {&lt;BR /&gt;auto imageAcc = imageBuffer.get_access&amp;lt;access::mode::read&amp;gt;(h);&lt;BR /&gt;auto filteredImageAcc = filteredImageBuffer.get_access&amp;lt;&amp;gt;(h);&lt;BR /&gt;auto filterAcc = filterBuf.get_access&amp;lt;access::mode::read&amp;gt;(h);&lt;BR /&gt;h.parallel_for(range&amp;lt;2&amp;gt;{height - ((filterHeight / 2) * 2), width - ((filterWidth / 2) * 2)}, [=](item&amp;lt;2&amp;gt; id) {&lt;BR /&gt;float value = 0;&lt;BR /&gt;auto global_row = id.get_id(0) + (filterHeight / 2);&lt;BR /&gt;auto global_column = id.get_id(1) + (filterWidth / 2);&lt;BR /&gt;//filter need a special case for edges, but here it just skip them&lt;/P&gt;&lt;P&gt;for (int i = 0; i &amp;lt; filterHeight; i++) {&lt;BR /&gt;for (int j = 0; j &amp;lt; filterWidth; j++) {&lt;BR /&gt;value += filterAcc[i][j] * imageAcc[(id.get_id(0) + i) * width + id.get_id(1) + j];&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;filteredImageAcc[global_row * width + global_column] = (uchar)(value);&lt;BR /&gt;});&lt;BR /&gt;});&lt;BR /&gt;q.wait();&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;cv::Mat filteredImageResult(height, width, CV_8UC1, filteredImageData.data());&lt;BR /&gt;cv::namedWindow("filtered Image", cv::WINDOW_NORMAL);&lt;BR /&gt;cv::imshow("filtered Image", filteredImageResult);&lt;BR /&gt;cv::namedWindow("original Image", cv::WINDOW_NORMAL);&lt;BR /&gt;cv::imshow("original Image", image);&lt;BR /&gt;cv::waitKey(0);&lt;/P&gt;&lt;P&gt;return 0;&lt;BR /&gt;}&lt;BR /&gt;```&lt;BR /&gt;the original image is attached&lt;/P&gt;</description>
      <pubDate>Mon, 29 Apr 2024 08:31:02 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/different-behavior-when-type-casting-from-float-to-uchar/m-p/1593419#M3692</guid>
      <dc:creator>Nabeel-Kh</dc:creator>
      <dc:date>2024-04-29T08:31:02Z</dc:date>
    </item>
    <item>
      <title>Re: different behavior when type casting from float to uchar</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/different-behavior-when-type-casting-from-float-to-uchar/m-p/1598008#M3744</link>
      <description>&lt;P&gt;Escalated to our engineering team for further investigation.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2024 19:54:30 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/different-behavior-when-type-casting-from-float-to-uchar/m-p/1598008#M3744</guid>
      <dc:creator>Alex_Y_Intel</dc:creator>
      <dc:date>2024-05-15T19:54:30Z</dc:date>
    </item>
    <item>
      <title>Re:different behavior when type casting from float to uchar</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/different-behavior-when-type-casting-from-float-to-uchar/m-p/1598107#M3752</link>
      <description>&lt;P&gt;Can you tell us how to build your code? Which libraries of opencv need to be linked?&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 16 May 2024 03:38:42 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/different-behavior-when-type-casting-from-float-to-uchar/m-p/1598107#M3752</guid>
      <dc:creator>cw_intel</dc:creator>
      <dc:date>2024-05-16T03:38:42Z</dc:date>
    </item>
    <item>
      <title>Re: Re:different behavior when type casting from float to uchar</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/different-behavior-when-type-casting-from-float-to-uchar/m-p/1598132#M3754</link>
      <description>&lt;P&gt;I did more tests, and I could locate where the error come from, in gpu the device will directly convert the float to uchar, any negative value will become 0 and values over 255 will become 255. however the cpu don't do that directly for some reason and it typecast the float to int first then to uchar, and when typecasting int to uchar it will only take the first 8 bits and ignore all the others similar to as using "value%256", that what lead to wrong results, I could reproduce the same wrong results in gpu by typecasting the value(float) to int then to uchar.&lt;BR /&gt;the error isn't related to opencv, using uchar array with buffer and accessor to it will give same results.&lt;BR /&gt;not sure if the error considered from the compiler or from the code itself.&lt;/P&gt;</description>
      <pubDate>Thu, 16 May 2024 05:58:27 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/different-behavior-when-type-casting-from-float-to-uchar/m-p/1598132#M3754</guid>
      <dc:creator>Nabeel-Kh</dc:creator>
      <dc:date>2024-05-16T05:58:27Z</dc:date>
    </item>
    <item>
      <title>Re:different behavior when type casting from float to uchar</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/different-behavior-when-type-casting-from-float-to-uchar/m-p/1598186#M3755</link>
      <description>&lt;P&gt;Thank you for the details. Based on your description, I reproduced your issue with a small reproducer. And reported this issue to our internal team. Will let you know when there is any update.&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 16 May 2024 08:15:48 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/different-behavior-when-type-casting-from-float-to-uchar/m-p/1598186#M3755</guid>
      <dc:creator>cw_intel</dc:creator>
      <dc:date>2024-05-16T08:15:48Z</dc:date>
    </item>
    <item>
      <title>Re:different behavior when type casting from float to uchar</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/different-behavior-when-type-casting-from-float-to-uchar/m-p/1604049#M3819</link>
      <description>&lt;P&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, &amp;quot;Segoe UI&amp;quot;, Roboto, Oxygen, Ubuntu, &amp;quot;Fira Sans&amp;quot;, &amp;quot;Droid Sans&amp;quot;, &amp;quot;Helvetica Neue&amp;quot;, sans-serif;"&gt;It is not an issue. It's undefined behavior since negative value and values over 255  can not be represented by unsigned char according to&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://developercommunity.visualstudio.com/t/different-values-when-cast-negative-double-to-unsi/1161038#TPIN-N1166151" rel="noopener noreferrer" target="_blank" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, &amp;quot;Segoe UI&amp;quot;, Roboto, Oxygen, Ubuntu, &amp;quot;Fira Sans&amp;quot;, &amp;quot;Droid Sans&amp;quot;, &amp;quot;Helvetica Neue&amp;quot;, sans-serif;"&gt;https://developercommunity.visualstudio.com/t/different-values-when-cast-negative-double-to-unsi/1161038#TPIN-N1166151&lt;/A&gt;&lt;SPAN style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, &amp;quot;Segoe UI&amp;quot;, Roboto, Oxygen, Ubuntu, &amp;quot;Fira Sans&amp;quot;, &amp;quot;Droid Sans&amp;quot;, &amp;quot;Helvetica Neue&amp;quot;, sans-serif;"&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, &amp;quot;Segoe UI&amp;quot;, Roboto, Oxygen, Ubuntu, &amp;quot;Fira Sans&amp;quot;, &amp;quot;Droid Sans&amp;quot;, &amp;quot;Helvetica Neue&amp;quot;, sans-serif;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 05 Jun 2024 08:58:53 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/different-behavior-when-type-casting-from-float-to-uchar/m-p/1604049#M3819</guid>
      <dc:creator>cw_intel</dc:creator>
      <dc:date>2024-06-05T08:58:53Z</dc:date>
    </item>
  </channel>
</rss>

