用findContours(cv2)切片图像

时间:2019-01-25 15:25:38

标签: python opencv image-processing

由于声誉问题,我无法发布图片,因此,这是我下载的图片的链接:

https://www.amvplaygrounds.co.uk/pub/media/catalog/product/a/m/amv_f4-pm-017-shapes-circle-square-rectangle-star-triangle-200mm-2-sq-3d.jpg

import cv2
import numpy as np
import matplotlib.pyplot as plt 
image = cv2.imread(r'C:\Users\User\Desktop\shapes.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 200, 255, 0)
contours, hier = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    x,y,w,h = cv2.boundingRect(cnt)
    shape = thresh[y:y+h,x:x+w]
    plt.imshow(shape)

它只会返回一个轮廓,而不是您在图片中看到的6。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

ERROR at line 58: ORA-06550: line 58, column 53: PLS-00487: Invalid reference to variable 'FK' ORA-06550: line 58, column 9: PL/SQL: Statement ignored ORA-06550: line 59, column 46: PLS-00487: Invalid reference to variable 'FK' ORA-06550: line 59, column 9: PL/SQL: Statement ignored 黑色背景下查找白色对象,因此您需要像这样设置阈值:

v_program_sec VARCHAR2(50);     
cntr INTEGER :=0;   
TYPE tab_name IS TABLE OF all_constraints.table_name%TYPE; 
tab_names tab_name; 
 cursor all_cons is
 select table_name
 from ALL_CONSTRAINTS
 where constraint_type = 'R'
 and r_constraint_name =
 (select constraint_name
 from ALL_CONSTRAINTS
 where constraint_type='P'
 and table_name='POLICIES')
 and table_name not in ('POLICY_AMOUNTS',
 'POLICY_DATES','FUND_POLICIES',
 'PNDNG_UW_REQUIREMENTS','RIDERS','TEMP_PARTIES',
 'POLICY_AMOUNTS_HISTORY','AGREEMENTS','NFHST','VLRP_RIDER');
fk PLS_INTEGER; 
procedure log_error 
    (p_err_cd IN NUMBER, p_err_msg IN VARCHAR2,
     p_program_sec IN VARCHAR2, p_ref_num IN VARCHAR2)
  is
  begin
    rollback;
    insert into LOAD_ERROR_LOG
      (err_cd, err_msg, program_sec, rec_hit)
    values
      (p_err_cd, p_err_msg, p_program_sec,
       'VLDM1060 '||p_ref_num||to_char(SYSDATE,' hh24:mi mmddyyyy'));
    commit;
  end;
BEGIN 
OPEN all_cons; 
FETCH all_cons BULK COLLECT INTO tab_names; 
-- existing policies
  for infc_pol in 
    (select TEMP_POLICIES.*,
            POLICIES.rowid p_rowid, POLICIES.status_cd p_status_cd 
       from POLICIES, TEMP_POLICIES 
      where POLICIES.policy_num = TEMP_POLICIES.policy_num
        and TEMP_POLICIES.status_cd in ('0','1','2','3','4','5','6','7','8','L','Z')
      order by POLICIES.rowid
    )
  loop
    v_program_sec := 'Delete child records - INFC';
    declare
      del_cursor INTEGER;
      exe_cursor INTEGER;
      cntr       INTEGER :=0;

    begin
         for fk IN  1..tab_names.count  
      loop
        v_program_sec := 'Delete records from '||fk.table_name||' - INFC';
        EXECUTE IMMEDIATE 'delete from '||fk.table_name||
          ' where pcy_policy_num = '||''''||infc_pol.policy_num||'''';
      end loop;       
        v_program_sec := 'Delete records from RIDERS - INFC';
        delete from RIDERS
         where pcy_policy_num = infc_pol.policy_num; 
        v_program_sec := 'Delete records from PNDNG_UW_REQUIREMENTS - INFC';
        delete from PNDNG_UW_REQUIREMENTS
         where pcy_policy_num = infc_pol.policy_num;
        v_program_sec := 'Delete records from POLICY_AMOUNTS - INFC';
        delete from POLICY_AMOUNTS
         where amt_code <> '10'
           and pcy_policy_num = infc_pol.policy_num;
        v_program_sec := 'Delete records from POLICY_DATES - INFC';
        delete from POLICY_DATES
         where daty_code not in ('01','03','04','17','18','20','21')
           and pcy_policy_num = infc_pol.policy_num;
      v_program_sec := 'Update policy record - INFC'; 
      update POLICIES
         set status_cd = infc_pol.status_cd,
             paymnt_mode_cd = infc_pol.paymnt_mode_cd,
             company_cd = infc_pol.company_cd,
             fund_typ = infc_pol.fund_typ, 
             partcpng_ind = infc_pol.partcpng_ind,
             distrb_cd = infc_pol.distrb_cd, 
             product_typ = infc_pol.product_typ,
             restricted_ind = infc_pol.restricted_ind, 
             acct_exec_id = infc_pol.acct_exec_id, 
             rollover_ind = infc_pol.rollover_ind, 
             loan_ind = infc_pol.loan_ind,
             cod_ind = infc_pol.cod_ind, 
             billng_mthd_cd = infc_pol.billng_mthd_cd,
             db_option_cd = infc_pol.db_option_cd,
             iss_state_cd = infc_pol.iss_state_cd,
             reinsure_ind = infc_pol.reinsure_ind,
             restrctns_ind = infc_pol.restrctns_ind,
             tax_status_cd = infc_pol.tax_status_cd,
             cvat_ind = infc_pol.cvat_ind,
             plan_cd = infc_pol.plan_cd,
             payor_grp_num = infc_pol.payor_grp_num, 
             last_update_dt = infc_pol.last_update_dt,
             spec_risk_ind = infc_pol.spec_risk_ind
       where policy_num = infc_pol.policy_num;
       commit;
       exception
      when OTHERS then
        declare
          error_code NUMBER := SQLCODE;
          error_msg  VARCHAR2(300) := SQLERRM;
        begin
          log_error(error_code, error_msg, v_program_sec, infc_pol.policy_num);
        end;
    CLOSE all_cons; 
    end;  
  end loop;  
  -- new policies
  begin
    v_program_sec := 'insert new INFC policies from TEMP_POLICIES';
    insert into policies
      (policy_num, status_cd, paymnt_mode_cd, company_cd,
       fund_typ, partcpng_ind, distrb_cd, product_typ,
       restricted_ind, acct_exec_id, rollover_ind, loan_ind,
       cod_ind, billng_mthd_cd, db_option_cd, iss_state_cd,
       reinsure_ind, restrctns_ind, tax_status_cd, cvat_ind,
       plan_cd, payor_grp_num, last_update_dt, spec_risk_ind)
    select 
       policy_num, status_cd, paymnt_mode_cd, company_cd,
       fund_typ, partcpng_ind, distrb_cd, product_typ,
       restricted_ind, acct_exec_id, rollover_ind, loan_ind,
       cod_ind, billng_mthd_cd, db_option_cd, iss_state_cd,
       reinsure_ind, restrctns_ind, tax_status_cd, cvat_ind,
       plan_cd, payor_grp_num, last_update_dt, spec_risk_ind
      from TEMP_POLICIES tp
     where not exists (select 'x'
                  from POLICIES
                 where policy_num = tp.policy_num)
       and status_cd in ('0','1','2','3','4','5','6','7','8','L','Z');
    commit;
  exception
    when OTHERS then
      declare
        error_code NUMBER := SQLCODE;
        error_msg  VARCHAR2(300) := SQLERRM;
      begin
        log_error(error_code, error_msg, v_program_sec, 'NEW');
      end;
  end;
end;```

,您需要找到这样的轮廓:

findContours()