hwratio=np.append(hwratio, props[label-1].major_axis_length/props[label-1].minor_axis_length) density_hull=np.append(density_hull, props[label-1].solidity) circularity=np.append(circularity, props[label-1].eccentricity) if len(density_cbcr) == 0: density_cbcr = -1*np.ones(1) if len(area_covered) == 0: area_covered = -1*np.ones(1) if len(hwratio) == 0: hwratio = -1*np.ones(1) if len(density_hull) == 0: density_hull = -1*np.ones(1) if len(circularity) == 0: circularity = -1*np.ones(1) labels_df['percent_skin_abg'][index] = sm.percent_skin(data.ImgReg.skin.abg) labels_df['percent_skin_cbcr'][index] = sm.percent_skin(data.ImgReg.skin.cbcr) labels_df['percent_skin_cccm'][index] = sm.percent_skin(data.ImgReg.skin.cccm) labels_df['percent_skin_equalized_abg'][index] = sm.percent_skin(data.ImgEq.skin.abg) labels_df['percent_skin_equalized_cbcr'][index] = sm.percent_skin(data.ImgEq.skin.cbcr) labels_df['percent_skin_equalized_cccm'][index] = sm.percent_skin(data.ImgEq.skin.cccm) labels_df['label_number'][index] = len(density_cbcr) labels_df['area_covered_max'][index] = max(area_covered) labels_df['area_covered_mode'][index] = float(stats.mode(area_covered)[0]) labels_df['density_max'][index] = max(density_cbcr) labels_df['density_mode'][index] = float(stats.mode(density_cbcr)[0]) labels_df['hwratio_max'][index] = max(hwratio) labels_df['hwratio_mode'][index] = float(stats.mode(hwratio)[0]) labels_df['density_hull_max'][index] = max(density_hull) labels_df['density_hull_mode'][index] = float(stats.mode(density_hull)[0]) labels_df['circularity_max'][index] = max(circularity)
image_EA_cbcr = data.ImgEqAdapt.skin.cbcr image_EA_cccm = data.ImgEqAdapt.skin.cccm labeled_abg, shapes_abg = change_morphology_label(image_EA_abg) labeled_cbcr, shapes_cbcr = change_morphology_label(image_EA_cbcr) labeled_cccm, shapes_cccm = change_morphology_label(image_EA_cccm) props_abg = measure.regionprops(labeled_abg) props_cbcr = measure.regionprops(labeled_cbcr) props_cccm = measure.regionprops(labeled_cccm) img_size = (labeled_abg.shape)[0]*(labeled_abg.shape)[1] features_abg = get_features(labeled_abg, props_abg, image_EA_abg, shapes_abg, img_size) features_cbcr = get_features(labeled_cbcr, props_cbcr, image_EA_cbcr, shapes_cbcr, img_size) features_cccm = get_features(labeled_cccm, props_cccm, image_EA_cccm, shapes_cccm, img_size) print "measure.regionprops done" labels_df.loc[len(labels_df.index)]=([sm.percent_skin(image_abg), sm.percent_skin(image_cbcr), sm.percent_skin(image_cccm),\ sm.percent_skin(image_EA_abg), sm.percent_skin(image_EA_cbcr), sm.percent_skin(image_EA_cccm),\ features_abg[0], features_abg[1], features_abg[2], features_abg[3], features_abg[4], features_abg[5],\ features_abg[6], features_abg[7], features_abg[8], features_abg[9], features_abg[10],\ features_cbcr[0], features_cbcr[1], features_cbcr[2], features_cbcr[3], features_cbcr[4], features_cbcr[5],\ features_cbcr[6], features_cbcr[7], features_cbcr[8], features_cbcr[9], features_cbcr[10],\ features_cccm[0], features_cccm[1], features_cccm[2], features_cccm[3], features_cccm[4], features_cccm[5],\ features_cccm[6], features_cccm[7], features_cccm[8], features_cccm[9], features_cccm[10]]) #print "label features added to data frame" labels_df.to_csv("Features_Food.csv")
labels_df["URL"] = urls index = -1 for url in urls: index += 1 print "index, url: %d, %s" % (index, url) try: read = urlopen(url).read() obj = Image.open(cStringIO.StringIO(read)) image = np.array(obj) if image.shape[2] == 3: # skip gray scale data = sm.SetUpImage(image) print "image shape:", image.shape labels_df["pskin_ImgReg_abg"][index] = sm.percent_skin(data.ImgReg.skin.abg) labels_df["pskin_ImgReg_cbcr"][index] = sm.percent_skin(data.ImgReg.skin.cbcr) labels_df["pskin_ImgReg_cccm"][index] = sm.percent_skin(data.ImgReg.skin.cccm) labels_df["pskin_ImgEq_abg"][index] = sm.percent_skin(data.ImgEq.skin.abg) labels_df["pskin_ImgEq_cbcr"][index] = sm.percent_skin(data.ImgEq.skin.cbcr) labels_df["pskin_ImgEq_cccm"][index] = sm.percent_skin(data.ImgEq.skin.cccm) labels_df["pskin_ImgAdapt_abg"][index] = sm.percent_skin(data.ImgEqAdapt.skin.abg) labels_df["pskin_ImgAdapt_cbcr"][index] = sm.percent_skin(data.ImgEqAdapt.skin.cbcr) labels_df["pskin_ImgAdapt_cccm"][index] = sm.percent_skin(data.ImgEqAdapt.skin.cccm) labels_df["pfill_ImgReg_abg"][index] = sm.fill_percent(data.ImgReg.skin.abg) labels_df["pfill_ImgReg_cbcr"][index] = sm.fill_percent(data.ImgReg.skin.cbcr) labels_df["pfill_ImgReg_cccm"][index] = sm.fill_percent(data.ImgReg.skin.cccm) labels_df["pfill_ImgEq_abg"][index] = sm.fill_percent(data.ImgEq.skin.abg) labels_df["pfill_ImgEq_cbcr"][index] = sm.fill_percent(data.ImgEq.skin.cbcr) labels_df["pfill_ImgEq_cccm"][index] = sm.fill_percent(data.ImgEq.skin.cccm)
for label in range(1, shapes+1): density = float(((labeled==label)&data.ImgReg.skin.cccm).sum())/float((labeled==label).sum()) #defines a region of interest if at least 50% of its pixels are skin color, but less than 100%, #and if the area is bigger than an arbritary number print "in label loop" if density>0.5 and density<1 and props[label-1].area > 50: density_cbcr = density area_covered = props[label-1].area/img_size hwratio = props[label-1].major_axis_length/props[label-1].minor_axis_length density_hull = props[label-1].solidity circularity = props[label-1].eccentricity labels_df.loc[len(labels_df.index)] = [label, area_covered, density_cbcr, hwratio, density_hull, circularity] print "label features added to data frame" # labels_df = pd.DataFrame(columns=['percent_skin','percent_skin_equalized','label_number', 'area_covered', 'density_cbcr', 'hwratio', 'density_hull', 'circularity']) labels_df["percent_skin"]=np.nan labels_df.percent_skin[0:3] = np.array([sm.percent_skin(data.ImgReg.skin.abg), \ sm.percent_skin(data.ImgReg.skin.cbcr), \ sm.percent_skin(data.ImgReg.skin.cccm)]) labels_df["percent_skin_equalized"]=np.nan labels_df.percent_skin_equalized[0:3] = np.array([sm.percent_skin(data.ImgEq.skin.abg), \ sm.percent_skin(data.ImgEq.skin.cbcr), \ sm.percent_skin(data.ImgEq.skin.cccm)]) print "percent skin added to df" labels_df.to_csv("Features.csv")
for url in urls: index += 1 print "index, url: %d -- %s" % (index,url) try: read = urlopen(url).read() obj = Image.open(cStringIO.StringIO(read)) img = np.array(obj) data = sm.SetUpImage(img) todd_map= data.SkinLikelihood() SkinFilter= sm.FilterSlice(todd_map,6) print "image read in" print url #percent skin-like pixels in each skinmap labels_df['percent_skin_SkinLikelihood_6'][index] = sm.percent_skin(SkinFilter) labels_df['percent_skin_abg'][index] = sm.percent_skin(data.ImgReg.skin.abg) labels_df['percent_skin_cbcr'][index] = sm.percent_skin(data.ImgReg.skin.cbcr) labels_df['percent_skin_equalized_cccm'][index] = sm.percent_skin(data.ImgReg.skin.cccm) labels_df['percent_skin_equalized_abg'][index] = sm.percent_skin(data.ImgEq.skin.abg) labels_df['percent_skin_equalized_cbcr'][index] = sm.percent_skin(data.ImgEq.skin.cbcr) labels_df['percent_skin_equalized_cccm'][index] = sm.percent_skin(data.ImgEq.skin.cccm) labels_df['percent_skin_adapt_abg'][index] = sm.percent_skin(data.ImgEqAdapt.skin.abg) labels_df['percent_skin_adapt_cbcr'][index] = sm.percent_skin(data.ImgEqAdapt.skin.cbcr) labels_df['percent_skin_adapt_cccm'][index] = sm.percent_skin(data.ImgEqAdapt.skin.cccm) #find blobs and measure properties image = SkinFilter #dilate,fill,erode struct1 = ndimage.generate_binary_structure(2, 2) img_dilated = ndimage.morphology.binary_dilation(image, iterations=1, structure=struct1)
def extract_features_and_feature_Figs_to_plot(n_images,nth_image,rgb_image,image_name): cols= [ 'UserUploadedImageName', 'percent_skin_SkinLikelihood_6', 'percent_skin_abg', 'percent_skin_cbcr', 'percent_skin_cccm', 'percent_skin_equalized_abg', 'percent_skin_equalized_cbcr', 'percent_skin_equalized_cccm', 'percent_skin_adapt_abg', 'percent_skin_adapt_cbcr', 'percent_skin_adapt_cccm', 'N_area_greater_HardCutoff', 'N_area_greater_floor', 'N_skin_pix_filled_greater_floor', 'N_amt_horiz_vert_lines_less_ceil', 'N_hull_filled_greater_floor', 'percent_area_gr_HardCutoff', 'percent_area_gr_floor', 'percent_area_skinpix_gr_floor', 'percent_area_horiz_vert_less_ceil', 'percent_area_hull_filled_gr_floor' ] labels_df=DataFrame() for name in cols: labels_df[name] = np.zeros(n_images) labels_df["UserUploadedImageName"]= image_name Figs= BlobFeaturesToPlot() #store blob feature images for imshow Figs.rgb_image= rgb_image obj= sm.SetUpImage(rgb_image) im= obj.SkinLikelihood() SkinFilter= FilterSlice(im,6) #6 is best value, 7 too retrictive, < 6 not great Figs.SkinLikelihood_6 = SkinFilter #percent of skin-like pixels in each skinmap labels_df['percent_skin_SkinLikelihood_6'][nth_image] = sm.percent_skin(SkinFilter) labels_df['percent_skin_abg'][nth_image] = sm.percent_skin(obj.ImgReg.skin.abg) labels_df['percent_skin_cbcr'][nth_image] = sm.percent_skin(obj.ImgReg.skin.cbcr) labels_df['percent_skin_equalized_cccm'][nth_image] = sm.percent_skin(obj.ImgReg.skin.cccm) labels_df['percent_skin_equalized_abg'][nth_image] = sm.percent_skin(obj.ImgEq.skin.abg) labels_df['percent_skin_equalized_cbcr'][nth_image] = sm.percent_skin(obj.ImgEq.skin.cbcr) labels_df['percent_skin_equalized_cccm'][nth_image] = sm.percent_skin(obj.ImgEq.skin.cccm) labels_df['percent_skin_adapt_abg'][nth_image] = sm.percent_skin(obj.ImgEqAdapt.skin.abg) labels_df['percent_skin_adapt_cbcr'][nth_image] = sm.percent_skin(obj.ImgEqAdapt.skin.cbcr) labels_df['percent_skin_adapt_cccm'][nth_image] = sm.percent_skin(obj.ImgEqAdapt.skin.cccm) image= SkinFilter fl= GetFloorsObj(image) #find blobs, consider only those containing > 900 skin pixels labels,n =ndimage.measurements.label(image,np.ones((3,3))) area = ndimage.measurements.sum(image,labels, index=np.arange(labels.max() + 1)) areaImg = area[labels] Figs.areaImg_AllBlobs= areaImg #cutoff, blobs must be sufficiently large ind_cutoff= np.where(areaImg < fl.HardAreaCutoff) areaImg[ind_cutoff] = 0. Figs.areaImg_AreaGrHardCutoff= areaImg props= measure.regionprops(areaImg.astype('int'),cache=True) ind_area_greater_floor=[] ind_skin_pix_filled_greater_floor=[] ind_amt_horiz_vert_lines_less_ceil=[] ind_hull_filled_greater_floor=[] for cnt in range(len(props)): blob= np.zeros(areaImg.shape).astype('bool') ind_blob= np.where(areaImg == props[cnt].label) blob[ind_blob]=True filled_blob= ndimage.morphology.binary_fill_holes(blob) if props[cnt].area > fl.AreaFloor: ind_area_greater_floor.append(cnt) if skin_pix_percent_filled(blob,filled_blob) > fl.SkinPercentGotFilledFloor: ind_skin_pix_filled_greater_floor.append(cnt) if percent_vert_horiz_lines(filled_blob,props[cnt].area) < fl.VertHorizCeil: ind_amt_horiz_vert_lines_less_ceil.append(cnt) if hull_percent_filled(filled_blob) > fl.HullPercentFilledFloor: ind_hull_filled_greater_floor.append(cnt) Figs.AreaGrFloor= getBlobsPropsIndex(areaImg,props,ind_area_greater_floor) Figs.SkinPixFilledGrFloor= getBlobsPropsIndex(areaImg,props,ind_skin_pix_filled_greater_floor) Figs.AmtHorizVertLessCeil= getBlobsPropsIndex(areaImg,props,ind_amt_horiz_vert_lines_less_ceil) Figs.HullFilledGreaterFloor= getBlobsPropsIndex(areaImg,props,ind_hull_filled_greater_floor) #fill labels labels_df['N_area_greater_HardCutoff'][nth_image]= len(props) labels_df['N_area_greater_floor'][nth_image]= len(ind_area_greater_floor) labels_df['N_skin_pix_filled_greater_floor'][nth_image]= len(ind_skin_pix_filled_greater_floor) labels_df['N_amt_horiz_vert_lines_less_ceil'][nth_image]= len(ind_amt_horiz_vert_lines_less_ceil) labels_df['N_hull_filled_greater_floor'][nth_image]= len(ind_hull_filled_greater_floor) #labels_df['EllipseFitResid_4HullFilled'][nth_image]= EllipseFitResid_4HullFilled #total percent area of all blobs in each grouping labels_df['percent_area_gr_HardCutoff'][nth_image]= percent_area_blobs_in_list(props,range(len(props)),SkinFilter) labels_df['percent_area_gr_floor'][nth_image]= percent_area_blobs_in_list(props,ind_area_greater_floor,SkinFilter) labels_df['percent_area_skinpix_gr_floor'][nth_image]= percent_area_blobs_in_list(props,ind_skin_pix_filled_greater_floor,SkinFilter) labels_df['percent_area_horiz_vert_less_ceil'][nth_image]= percent_area_blobs_in_list(props,ind_amt_horiz_vert_lines_less_ceil,SkinFilter) labels_df['percent_area_hull_filled_gr_floor'][nth_image]= percent_area_blobs_in_list(props,ind_hull_filled_greater_floor,SkinFilter) return labels_df,Figs