Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question If I have a float that becomes more than one value, how do I seperate them?

Discussion in 'Scripting' started by AstroSquirell, Nov 29, 2023.

  1. AstroSquirell

    AstroSquirell

    Joined:
    Dec 6, 2020
    Posts:
    2
    So I'm using this free plugin called OpenCV plus Unity (https://assetstore.unity.com/packages/tools/integration/opencv-plus-unity-85928#reviews) to try and find the diameters of circles when an image is uploaded. once it finds the diameters I want to be able to know which diameter is which. the images that are being uploaded contains a full circle on the right side of the screen and a circle with a hole in it(supposed to be representing a bearing) and I need a way of determining which is the outer and inner diameter of the circle, heres the code but I can't figure out how to seperate the float into it's seperate values, any help would be appreciated.
    this is the documentation I used to write my code: http://web.archive.org/web/20190414..._1_cv2.html#a5592236d8a56631270272610efac9ec0
    What I want is that if the radius is found on the left side of the screen than it is a coin but if it's found on the left side of the screen it finds the smaller of the two radius which means it's the inner radius

    The float I am finding is the radius float, when I play the app in the editor, and I upload a photo where it would find three radius and if I print or Debug.log it prints 3 different radius in the console, which is what I want, but if I try to find the smallest radius out of them it just thinks it's one float, like I tried using an if statement like this
    Code (CSharp):
    1.  if (radius > radius)
    2. {
    3.      //Do smt
    4. }
    But that doesn't really mean anything

    Code (CSharp):
    1.  var texture = DownloadHandlerTexture.GetContent(uwr);
    2. Mat image = Unity.TextureToMat(texture);
    3. //Gray scale image
    4. Mat grayMat = new Mat();
    5. Cv2.CvtColor(image, grayMat, ColorConversionCodes.BGR2GRAY);
    6. Mat thresh = new Mat();
    7. Cv2.Threshold(grayMat, thresh, 170, 255, ThresholdTypes.BinaryInv);
    8. // Extract Contours
    9. Point[][] contours;
    10. HierarchyIndex[] hierarchy;
    11. Cv2.FindContours(thresh, out contours, out hierarchy, RetrievalModes.Tree, ContourApproximationModes.ApproxNone, null);
    12. foreach (Point[] contour in contours)
    13. {
    14.      double length = Cv2.ArcLength(contour, true);
    15.      Point[] approx = Cv2.ApproxPolyDP(contour, length * 0.01 , true);
    16.  
    17.      Scalar colore;
    18.      colore = OpenCvSharp.Scalar.FromRgb(255, 0, 0);
    19.      if(length > 300)
    20.      {
    21.          Cv2.DrawContours(image, new Point[][] { approx }, 0, colore, 4);
    22.          Cv2.MinEnclosingCircle(approx, out center, out radius);
    23.          diameter = radius * 2;
    24.          if (center.X < Screen.width / 2)
    25.          {
    26.          
    27.          
    28.          
    29.              CoinDiamPx = diameter;
    30.              PxconversionValue = (CoinDiamPx/CoinDiamIn);
    31.              print(CoinDiamIn);
    32.              print("Coin Diameter: " + CoinDiamPx);
    33.              CoinD.SetText(CoinDiamIn.ToString());
    34.          }
    35.          else if (center.X > Screen.width / 2)
    36.          {
    37.          
    38.              InnerDiamPx = diameter;
    39.          
    40.              print(InnerDiamPx);
    41.              InnerD.SetText(InnerDiamPx.ToString() + " mm");
    42.          
    43.          }
    44.      
    45.       }
     
    Last edited: Nov 29, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,336
    A float is a single value. Not sure what you mean by this. Do you mean a vector?

    Otherwise...



    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?



    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - Do NOT attach entire scripts to your post.
    - ONLY post the relevant code, and then refer to it in your discussion.
     
  3. AstroSquirell

    AstroSquirell

    Joined:
    Dec 6, 2020
    Posts:
    2
    Sorry about that, I've updated my post, I think I made it so what I've done and what I'm looking for is clear, I've also added the documentation I've been using
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,336
    So let's review what you have:

    - line 12 iterates the contours it found

    - line 15 gets the approximate center of that one contour

    - line 22 appears to get the radius of whatever contour it found

    if you have an annulus, I think OpenCV would see it as two separate circles roughly located at the same point.

    is that what is happening?? find out by debugging!!


    Time to start debugging!

    By debugging you can find out exactly what your program is doing so you can fix it.

    Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    If your problem is with OnCollision-type functions, print the name of what is passed in!

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    If you are looking for how to attach an actual debugger to Unity: https://docs.unity3d.com/2021.1/Documentation/Manual/ManagedCodeDebugging.html

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.