SciSharp / SciSharp/NumSharp

How to use the repo to convert some Python code?

Open
#462 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
1.5k
Forks
205
Avg merge
7d 7h
Merged PRs (30d)
2

Description

Hello:
I have the following Python code to convert one RGB image to one YUV image, and use numpy to calculate an average of one column.

`import cv2
import numpy as np

img_rgb = cv2.imread('C:/Images/1.PNG')
img_yuv = cv2.cvtColor(img_rgb,cv2.COLOR_BGR2YUV)
averageV = np.average(img_yuv[:,:,2])
print(averageV);
`

The Python code works well. Now, I want to change it to use C# code, as I have many other C# programs will need this averageV value.

I have done the following:

  1. I created one C# console project with Visual Studio 2019 (target .NET 5.0)
  2. I installed necessary NUGET packages:
    PM> Install-Package OpenCvSharp4 -Version 4.5.3.20210817
    PM> Install-Package NumSharp -Version 0.30.0
  3. I have the following C# code:
    `using NumSharp;
    using OpenCvSharp;
    using System;

namespace ConvertRGB2YUV
{
class Program
{
public const string Image1_File = @"C:\Images\1.PNG";

    static void Main(string[] args)
    {
        Mat img_rgb = Cv2.ImRead(Image1_File);
        Mat img_yuv = img_rgb.CvtColor(ColorConversionCodes.RGB2YUV);
        //var averageV = (img_yuv[:,:,2]);
        //averageV = np.average(img_yuv[:,:, 2])
    }
}

}`

I can run my code, and I can see the image: img_rgb and img_yuv.
But I have no idea on how to write the python corresponding statement:
averageV = np.average(img_yuv[:,:,2])
In NumSharp, the img_yuv[…] simply doesn’t exist.
In Python, the img_yuv is treated like an array of float numbers.
How I can do this in NumSharp?
Please advise,
Thanks,

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no repository file, test, or entry point. Start by inspecting the NumSharp and OpenCvSharp usage shown in the C# example and compare it with the Python slicing and averaging expression. Done should be a confirmed repository-supported approach for obtaining the average V-channel value.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, numpy, python
Domain
computer-vision, data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.