Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. def crf(original_image, mask_img):
  2.  
  3. # Converting annotated image to RGB if it is Gray scale
  4. if(len(mask_img.shape)<3):
  5. mask_img = gray2rgb(mask_img)
  6. # #Converting the annotations RGB color to single 32 bit integer
  7. annotated_label = mask_img[:,:,0] + (mask_img[:,:,1]<<8) + (mask_img[:,:,2]<<16)
  8. # # Convert the 32bit integer color to 0,1, 2, ... labels.
  9. colors, labels = np.unique(annotated_label, return_inverse=True)
  10. n_labels = 2
  11. #Setting up the CRF model
  12. d = dcrf.DenseCRF2D(original_image.shape[1], original_image.shape[0], n_labels)
  13. # get unary potentials (neg log probability)
  14. U = unary_from_labels(labels, n_labels, gt_prob=0.7, zero_unsure=False)
  15. d.setUnaryEnergy(U)
  16. # This adds the color-independent term, features are the locations only.
  17. d.addPairwiseGaussian(sxy=(3, 3), compat=3, kernel=dcrf.DIAG_KERNEL,
  18. normalization=dcrf.NORMALIZE_SYMMETRIC)
  19. #Run Inference for 10 steps
  20. Q = d.inference(10)
  21. # Find out the most probable class for each pixel.
  22. MAP = np.argmax(Q, axis=0)
  23. return MAP.reshape((original_image.shape[0],original_image.shape[1]))
  24.  
  25. def crf(original_image, mask_img):
  26. labels = mask_img
  27. n_labels = 4
  28. #Setting up the CRF model
  29. d = dcrf.DenseCRF2D(original_image.shape[1], original_image.shape[0], n_labels)
  30. # get unary potentials (neg log probability)
  31. U = unary_from_labels(labels, n_labels, gt_prob=0.7, zero_unsure=False)
  32. d.setUnaryEnergy(U)
  33. # This adds the color-independent term, features are the locations only.
  34. d.addPairwiseGaussian(sxy=(3, 3), compat=3, kernel=dcrf.DIAG_KERNEL,
  35. normalization=dcrf.NORMALIZE_SYMMETRIC)
  36. #Run Inference for 10 steps
  37. Q = d.inference(10)
  38. # Find out the most probable class for each pixel.
  39. MAP = np.argmax(Q, axis=0)
  40. return MAP.reshape((original_image.shape[0],original_image.shape[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement