如何在Matplotlib中填充半透明颜色的区域?

时间:2017-01-21 21:05:55

标签: python matplotlib

许多关于图像分割的论文提供了每个片段都覆盖半透明色面膜的示例:

enter image description here

如果我有图像和面具,我可以在Matplotlib中获得相同的结果吗?

编辑:

在我的情况下,面具被定义为一个数字,其宽度和高度与填充数字0到(num_segments + 1)的图像相同,其中0表示"不应用任何颜色&#34 34;和其他数字意味着"用一些明显的颜色覆盖这个像素"。然而,如果掩码的另一种表示更合适,我可以尝试转换为它。

以下是我在此任务中发现的一些复杂问题,因此听起来并非如此:

  1. 彩色区域不是常规形状,如线条,方形或圆形,因此plot(..., 'o')fill()fill_between()等功能不起作用。它们甚至不是contours(或者至少我不知道如何在这里应用它们)。
  2. 修改Alpha通道并不是剧情中最受欢迎的东西,因此在Matplotlib的文档中很少提及。

1 个答案:

答案 0 :(得分:3)

这肯定可以做到。实施将取决于您的面具的样子。

这是一个例子

     function initMap() {
if (navigator.geolocation){
    navigator.geolocation.getCurrentPosition(function(position){
      var map = new google.maps.Map(document.getElementById('map'), {
          mapTypeControl: false,
          center: {lat:position.coords.latitude, lng:position.coords.longitude},
          zoom: 10
        });
  var panorama = new google.maps.StreetViewPanorama(
      document.getElementById('pano'), {
        position: {lat:position.coords.latitude, lng:position.coords.longitude},
        pov: {
          heading: 34,
          pitch: 10
        }
      });
      var usermarker = new google.maps.Marker({
  position: {lat:position.coords.latitude, lng:position.coords.longitude},
    draggable: true,
  map: map,
  icon: 'http://x3.cdn03.imgwykop.pl/c3201142/comment_aBtolaUIxKS7cvUnii43PWDPT3Lqduc2,w400.jpg',
});
// test marker
     var marker2 = new google.maps.Marker({
  position: {lat:50.671064, lng:17.926138},
    draggable: true,
  map: map,
  icon: 'http://emojipedia-us.s3.amazonaws.com/cache/1e/4d/1e4d8093e7011575d9266598a06d8ecb.png',
});
 });
        new AutocompleteDirectionsHandler(map);
      }
 /**
        * @constructor
       */
      function AutocompleteDirectionsHandler(map) {
        this.map = map;
        this.originPlaceId = null;
        this.destinationPlaceId = null;
        this.travelMode = 'WALKING';
        var originInput = document.getElementById('origin-input');
        var destinationInput = document.getElementById('destination-input');
        var modeSelector = document.getElementById('mode-selector');
        this.directionsService = new google.maps.DirectionsService;
        this.directionsDisplay = new google.maps.DirectionsRenderer;
        this.directionsDisplay.setMap(map);
        var originAutocomplete = new google.maps.places.Autocomplete(
            originInput, {placeIdOnly: true});
        var destinationAutocomplete = new google.maps.places.Autocomplete(
            destinationInput, {placeIdOnly: true});
        this.setupClickListener('changemode-walking', 'WALKING');
        this.setupClickListener('changemode-transit', 'TRANSIT');
        this.setupClickListener('changemode-driving', 'DRIVING');
        this.setupPlaceChangedListener(originAutocomplete, 'ORIG');
        this.setupPlaceChangedListener(destinationAutocomplete, 'DEST');
        this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(originInput);
        this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(destinationInput);
        this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(modeSelector);
      }
     //everything works until this moment
      AutocompleteDirectionsHandler.prototype.setupClickListener = function(id, mode) {
        var radioButton = document.getElementById(id);
        var me = this;
        radioButton.addEventListener('click', function() {
          me.travelMode = mode;
          me.route();
        });
      };
      AutocompleteDirectionsHandler.prototype.setupPlaceChangedListener = function(autocomplete, mode) {
        var me = this;
        autocomplete.bindTo('bounds', this.map);
        autocomplete.addListener('place_changed', function() {
          var place = autocomplete.getPlace();
          if (!place.place_id) {
            window.alert("Please select an option from the dropdown list.");
            return;
          }
          if (mode === 'ORIG') {
            me.originPlaceId = place.place_id;
          } else {
            me.destinationPlaceId = place.place_id;
          }
          me.route();
        });
      };
      AutocompleteDirectionsHandler.prototype.route = function() {
        if (!this.originPlaceId || !this.destinationPlaceId) {
          return;
        }
        var me = this;

        this.directionsService.route({
          origin: {'placeId': this.originPlaceId},
          destination: {'placeId': this.destinationPlaceId},
          travelMode: this.travelMode
        }, function(response, status) {
          if (status === 'OK') {
            me.directionsDisplay.setDirections(response);
          } else {
            window.alert('Directions request failed due to ' + status);
          }
         //   map.setStreetView(panorama);
        });
      };
}

enter image description here