如何访问凸包内部的像素?

一旦获得图像的凸包,它们是否可以通过任何方式来对位于凸包内的像素进行计数。

apiVersion: v1
kind: Namespace
metadata:
  name: spinnaker

---

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: spinnaker-admin
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: Serviceaccount
  name: default
  namespace: spinnaker

---

apiVersion: v1
kind: PersistentVolumeclaim
metadata:
  name: halyard-pv-claim
  namespace: spinnaker
  labels:
    app: halyard-storage-claim
spec:
  accessModes:
  - ReadWriteonce
  resources:
    requests:
      storage: 10Gi
  storageclassname: standard

---

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: spin-halyard
  namespace: spinnaker
  labels:
    app: spin
    stack: halyard
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: spin
      stack: halyard
  template:
    metadata:
      labels:
        app: spin
        stack: halyard
    spec:
      securityContext:
        runAsGroup: 1000
        fsGroup: 1000
      containers:
      - name: halyard-daemon
        # todo - make :stable or digest of :stable
        image: gcr.io/spinnaker-marketplace/halyard:stable
        imagePullPolicy: Always
        command:
        - /bin/sh
        args:
        - -c
        # We persist the files on a PersistentVolume. To have sane defaults,# we initialise those files from a ConfigMap if they don't already exist.
        - "test -f /home/spinnaker/.hal/config || cp -R /home/spinnaker/staging/.hal/. /home/spinnaker/.hal/ && /opt/halyard/bin/halyard"
        readinessProbe:
          exec:
            command:
            - wget
            - -q
            - --spider
            - http://localhost:8064/health
        ports:
        - containerPort: 8064
        volumeMounts:
        - name: persistentconfig
          mountPath: /home/spinnaker/.hal
        - name: halconfig
          mountPath: /home/spinnaker/staging/.hal/config
          subPath: config
        - name: halconfig
          mountPath: /home/spinnaker/staging/.hal/default/service-settings/deck.yml
          subPath: deck.yml
        - name: halconfig
          mountPath: /home/spinnaker/staging/.hal/default/service-settings/gate.yml
          subPath: gate.yml
        - name: halconfig
          mountPath: /home/spinnaker/staging/.hal/default/service-settings/igor.yml
          subPath: igor.yml
        - name: halconfig
          mountPath: /home/spinnaker/staging/.hal/default/service-settings/fiat.yml
          subPath: fiat.yml
        - name: halconfig
          mountPath: /home/spinnaker/staging/.hal/default/service-settings/redis.yml
          subPath: redis.yml
        - name: halconfig
          mountPath: /home/spinnaker/staging/.hal/default/profiles/front50-local.yml
          subPath: front50-local.yml
      volumes:
      - name: halconfig
        configMap:
          name: halconfig
      - name: persistentconfig
        persistentVolumeclaim:
          claimName: halyard-pv-claim
---

apiVersion: v1
kind: Service
metadata:
  name: spin-halyard
  namespace: spinnaker
spec:
  ports:
    - port: 8064
      targetPort: 8064
      protocol: TCP
  selector:
    app: spin
    stack: halyard

---

apiVersion: v1
kind: ConfigMap
metadata:
  name: halconfig
  namespace: spinnaker
data:
  igor.yml: |
    enabled: false
    skipLifeCycleManagement: true
  fiat.yml: |
    enabled: false
    skipLifeCycleManagement: true
  front50-local.yml: |
    spinnaker.s3.versioning: false
  gate.yml: |
    host: 0.0.0.0
  deck.yml: |
    host: 0.0.0.0
    env:
      API_HOST: http://spin-gate.spinnaker:8084
  redis.yml: |
    overrideBaseUrl: redis://spin-redis:6379
  config: |
    currentDeployment: default
    deploymentConfigurations:
    - name: default
      version: 1.12.2
      providers:
        appengine:
          enabled: false
          accounts: []
        aws:
          enabled: false
          accounts: []
          defaultKeyPairTemplate: '{{name}}-keypair'
          defaultRegions:
          - name: us-west-2
          defaults:
            iamRole: BaseIAMRole
        azure:
          enabled: false
          accounts: []
          bakeryDefaults:
            templateFile: azure-linux.json
            baseImages: []
        dcos:
          enabled: false
          accounts: []
          clusters: []
        dockerRegistry:
          enabled: false
          accounts: []
        google:
          enabled: false
          accounts: []
          bakeryDefaults:
            templateFile: gce.json
            baseImages: []
            zone: us-central1-f
            network: default
            useInternalIp: false
        kubernetes:
          enabled: true
          accounts:
          - name: my-kubernetes-account
            requiredgroupmembership: []
            providerVersion: V2
            dockerRegistries: []
            configureImagePullSecrets: true
            serviceaccount: true
            namespaces: []
            omitNamespaces: []
            kinds: []
            omitKinds: []
            customResources: []
            oAuthScopes: []
          primaryaccount: my-kubernetes-account
        oraclebmcs:
          enabled: false
          accounts: []
      deploymentEnvironment:
        size: SMALL
        type: Distributed
        accountName: my-kubernetes-account
        updateVersions: true
        consul:
          enabled: false
        vault:
          enabled: false
        customSizing: {}
        gitConfig:
          upstreamUser: spinnaker
      persistentStorage:
        persistentStoreType: s3
        azs: {}
        gcs:
          rootFolder: front50
        redis: {}
        s3:
          bucket: spinnaker-artifacts
          rootFolder: front50
          endpoint: http://minio-service.spinnaker:9000
          accessKeyId: dont-use-this
          secretaccessKey: for-production
        oraclebmcs: {}
      features:
        auth: false
        fiat: false
        chaos: false
        entityTags: false
        jobs: false
        artifacts: true
      metricStores:
        datadog:
          enabled: false
        prometheus:
          enabled: false
          add_source_metalabels: true
        stackdriver:
          enabled: false
        period: 30
        enabled: false
      notifications:
        slack:
          enabled: false
      timezone: America/Los_Angeles
      ci:
        jenkins:
          enabled: false
          masters: []
        travis:
          enabled: false
          masters: []
      security:
        apiSecurity:
          ssl:
            enabled: false
          overrideBaseUrl: /gate
        uiSecurity:
          ssl:
            enabled: false
        authn:
          oauth2:
            enabled: false
            client: {}
            resource: {}
            userInfoMapping: {}
          saml:
            enabled: false
          ldap:
            enabled: false
          x509:
            enabled: false
          enabled: false
        authz:
          groupmembership:
            service: EXTERNAL
            google:
              roleProviderType: GOOGLE
            github:
              roleProviderType: GITHUB
            file:
              roleProviderType: FILE
          enabled: false
      artifacts:
        gcs:
          enabled: false
          accounts: []
        github:
          enabled: false
          accounts: []
        http:
          enabled: false
          accounts: []
      pubsub:
        google:
          enabled: false
          subscriptions: []
      canary:
        enabled: true
        serviceIntegrations:
        - name: google
          enabled: false
          accounts: []
          gcsEnabled: false
          stackdriverEnabled: false
        - name: prometheus
          enabled: false
          accounts: []
        - name: datadog
          enabled: false
          accounts: []
        - name: signalfx
          enabled: false
          accounts: []
        - name: newrelic
          enabled: false
          accounts: []
        - name: aws
          enabled: true
          accounts:
          - name: kayenta-minio
            bucket: spinnaker-artifacts
            rootFolder: kayenta
            endpoint: http://minio-service.spinnaker:9000
            accessKeyId: dont-use-this
            secretaccessKey: for-production
            supportedTypes:
            - CONFIGURATION_STORE
            - OBJECT_STORE
          s3Enabled: true
        reduxLoggerEnabled: true
        defaultJudge: NetflixACAJudge-v1.0
        stagesEnabled: true
        templatesEnabled: true
        showAllConfigsEnabled: true
---

apiVersion: v1
kind: PersistentVolumeclaim
metadata:
  name: minio-pv-claim
  namespace: spinnaker
  labels:
    app: minio-storage-claim
spec:
  accessModes:
  - ReadWriteonce
  resources:
    requests:
      storage: 10Gi
  storageclassname: standard

---

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  # This name uniquely identifies the Deployment
  name: minio-deployment
  namespace: spinnaker
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: minio
    spec:
      volumes:
      - name: storage
        persistentVolumeclaim:
          claimName: minio-pv-claim
      containers:
      - name: minio
        image: minio/minio
        args:
        - server
        - /storage
        env:
        - name: MINIO_accESS_KEY
          value: "dont-use-this"
        - name: MINIO_SECRET_KEY
          value: "for-production"
        ports:
        - containerPort: 9000
        volumeMounts:
        - name: storage
          mountPath: /storage

---

apiVersion: v1
kind: Service
metadata:
  name: minio-service
  namespace: spinnaker
spec:
  ports:
    - port: 9000
      targetPort: 9000
      protocol: TCP
  selector:
    app: minio

---

apiVersion: batch/v1
kind: Job
metadata:
  name: hal-deploy-apply
  namespace: spinnaker
  labels:
    app: job
    stack: hal-deploy
spec:
  template:
    metadata:
      labels:
        app: job
        stack: hal-deploy
    spec:
      restartPolicy: OnFailure
      containers:
      - name: hal-deploy-apply
        # todo use a custom image
        image: gcr.io/spinnaker-marketplace/halyard:stable
        command:
        - /bin/sh
        args:
        - -c
        - "hal deploy apply --daemon-endpoint http://spin-halyard.spinnaker:8064"

在这里,import numpy as np import cv2 import PIL usg = cv2.imread("1.jpg",0) ret,threshold = cv2.threshold(usg,10,255,cv2.THRESH_BINARY) _,contours,_ = cv2.findContours(threshold,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) hull = [cv2.convexHull(c) for c in contours] final = cv2.drawContours(usg,hull,-1,(255,255)) 给出图像的凸包。之后,我要访问船体内部的像素。

okgame 回答:如何访问凸包内部的像素?

我首先通过在黑色框架上用cv2.fillPoly()将我的凸包白色绘制为白色来解决这个问题

  1. 首先创建跟随框架形状的黑色框架
    black_frame = np.zeros_like(your_frame).astype(np.uint8)
  2. 用白色油漆凸包
    cv2.fillPoly(black_frame,[hull],(255,255,255))
  3. 使用numpy布尔索引创建蒙版,它将生成一个内部包含True / False值的蒙版,如果像素值为白色,则为True
    mask = black_frame == 255
  4. 您可以通过获取框架和蒙版之间的乘积来访问像素值,如果为False,则值将为
    targetROI = your_frame * mask
  5. 使用遮罩访问像素。
black_frame = np.zeros_like(your_frame).astype(np.uint8)
cv2.fillPoly(black_frame,255))
mask = black_frame == 255
targetROI = your_frame * mask
,

如果已经具有绘制的凸包,则只需使用NumPy的boolean array indexing即可访问凸包内的像素。

这是一些示例代码:

import cv2
import numpy as np

# Set up dummy image
img = np.zeros((400,400,3),np.uint8)
cv2.fillPoly(img,np.array([[[20,20],[120,40],[280,30],350],[140,320],[30,380]]]),(0,255))

# Threshold image
_,img_thr = cv2.threshold(cv2.cvtColor(img,cv2.COLOR_BGR2GRAY),10,cv2.THRESH_BINARY)

# Find contours: OpenCV 4.x
contours,_ = cv2.findContours(img_thr,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

# Find contours: OpenCV 3.x
#_,contours,cv2.CHAIN_APPROX_NONE)

# Convex hulls from contours in list
hulls = [cv2.convexHull(c) for c in contours]

# Draw convex hull of first contour
hull = np.zeros(img_thr.shape,np.uint8)
hull = cv2.drawContours(hull,np.array([np.squeeze(hulls[0])]),-1,cv2.FILLED)

# Access pixels inside convex hull; here add some green values to original image
idx = hull == 255
final = img.copy()
final[idx,:] = final[idx,:] + (0,128,0)

cv2.imshow('img',img)
cv2.imshow('hull',hull)
cv2.imshow('final',final)
cv2.waitKey(0)
cv2.destroyAllWindows()

那是伪图像img

Image

这是绘制的凸包hull

Convex hull

这是最终图像final,其中img中凸包内的像素已被修改:

Final

希望有帮助!

,

凸包是凸多边形。您可以通过实现多边形填充算法来枚举船体内部的像素。对于凸多边形,使用扫描线方法很容易。

  • 找到一个最高顶点,并注意从其发出的两个侧面;

  • 在纵坐标上向下循环;

  • 对于每个纵坐标,确保水平线与左右两侧相交;如果不是,则向下转到下一个侧面;

  • 将左路口加入到右路口;该段上的所有像素都在船壳内;

  • 在左右两侧相遇时停止。

enter image description here

本文链接:https://www.f2er.com/3096176.html

大家都在问