Простое повторение через перечисление Целого типа?

enter image description here

Демонстрация Python для поиска и рисования повернутого объекта:

# 2019/03/01
# https://stackoverflow.com/a/54942835/3547485

import numpy as np
import cv2

gray = cv2.imread("tmp.png", cv2.IMREAD_GRAYSCALE)
th, threshed = cv2.threshold(gray, 220, 255, cv2.THRESH_BINARY_INV)
cnts = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2]
cnt = sorted(cnts, key=cv2.contourArea, reverse=True)[0]
rbox = cv2.minAreaRect(cnt)
pts = cv2.boxPoints(rbox).astype(np.int32)
cv2.drawContours(img, [pts], -1, (0, 255, 0), 1, cv2.LINE_AA)

cv2.imwrite("dst.png", img)

Полезные функции OpenCV (в Python): cv2. minAreaRect, cv2.boxPoints, cv.2drawContours. Вы можете найти соответствующие функции в Java.

5
задан Frosty840 30 April 2009 в 07:25
поделиться

5 ответов

Извините за C #:

static IEnumerable<T> EnumRange<T>(T begin, T end)
{
    T[] values = (T[])Enum.GetValues(typeof(T));
    int beginIndex = Array.IndexOf(values, begin);
    int endIndex = Array.IndexOf(values, end);

    for(int i = beginIndex; i <= endIndex; ++i)
        yield return values[i];
}

foreach(MyEnum e in EnumRange(MyEnum.First, MyEnum.Fourth)
    // Code goes here
3
ответ дан 14 December 2019 в 04:47
поделиться

Посмотрите на Enum.GetValues ​​(Type)

1
ответ дан 14 December 2019 в 04:47
поделиться

Марк в основном прав, за исключением того, что вам не нужно явно сортировать (Enum.GetValues ​​уже возвращает отсортированный), и вам нужно привести к выводу:

enum myEnum
{
    first = 0x1,
    second = 0x2,
    third = 0x4,
    fourth = 0x8,
    fifth = 0x10,
    sixth = 0x20,
}

public static void Main(string[] a)
{
    var qry = from myEnum value in Enum.GetValues(typeof(myEnum))
        where value >= myEnum.first && value <= myEnum.fourth
        select value;
    foreach(var value in qry)
    {
        Console.WriteLine((int)value);
    }
}
2
ответ дан 14 December 2019 в 04:47
поделиться

Я ожидаю, что вам нужно будет использовать Enum.GetValues ​​() и фильтровать результаты по вашему желанию. Например, используя LINQ) (и используя C #, поскольку я недостаточно хорошо знаю VB):

(примечание: здесь важна вещь GetValues ​​ - которая должна быть идентична между VB и C # )

static void Main() {
    var qry = from MyEnum value in Enum.GetValues(typeof(MyEnum))
              where value >= MyEnum.First && value <= MyEnum.Fourth
              orderby value // to sort by value; remove otherwise
              select value;

    foreach (var value in qry) {
        Console.WriteLine(value);
    }
}
enum MyEnum { 
    First = 0x01,
    Second = 0x02,
    Third = 0x04,
    Fourth = 0x08,
    Fifth = 0x10,
    Sixth = 0x20
}
1
ответ дан 14 December 2019 в 04:47
поделиться

Я знаю, что на это уже был дан ответ, но мне кажется, что это было продумано. Почему бы не сделать это?

    Private Shared Sub Main()

        Dim aryEnums As Array = [Enum].GetValues(GetType(myEnum))

        For i As Integer = 0 To aryEnums.GetLength(0) - 1

            Console.WriteLine(aryEnums(i))

        Next

    End Sub
1
ответ дан 14 December 2019 в 04:47
поделиться
Другие вопросы по тегам:

Похожие вопросы: