[null,null,["最后更新时间 (UTC):2025-07-27。"],[[["\u003cp\u003eEarth Engine allows for the creation of image composites from image collections using various methods, including selecting the most recent pixel or using reducers like the median.\u003c/p\u003e\n"],["\u003cp\u003eMasking can be applied to composites to highlight specific features, such as masking water to focus on land areas.\u003c/p\u003e\n"],["\u003cp\u003eCustom mosaics can be generated by combining different visualization images, allowing for the representation of different features with distinct colors or styles.\u003c/p\u003e\n"],["\u003cp\u003eEarth Engine handles large datasets efficiently, enabling complex compositing and mosaicking operations in a short amount of time.\u003c/p\u003e\n"],["\u003cp\u003eVisualization images can be created using the \u003ccode\u003evisualize()\u003c/code\u003e function to display images with specific parameters.\u003c/p\u003e\n"]]],[],null,["# Compositing, Masking, and Mosaicking\n\nWith the [Landsat 8 TOA\nreflectance collection](/earth-engine/datasets/catalog/LANDSAT_LC8_L1T_TOA) loaded into a variable called `l8`, you saw that the\nfollowing code results in a recent-value composite:\n\n### Code Editor (JavaScript)\n\n```javascript\nvar l8 = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA');\nvar landsat2016 = l8.filterDate('2016-01-01', '2016-12-31');\nMap.addLayer(landsat2016, visParams, 'l8 collection');\n```\n\nOne of the problems with this composite is that it's full of clouds. Instead of just\ntaking the last pixel in the collection (when you add a collection to the map, Earth\nEngine implicitly calls [`mosaic()`](/earth-engine/apidocs/ee-imagecollection-mosaic)\non it), you can *reduce* the `ImageCollection`\n([Learn more about reducing image collections](/earth-engine/guides/reducers_image_collection)).\n\nCompositing with Reducers\n-------------------------\n\nYou were first introduced to reducers for [getting\nstatistics in an image region](/earth-engine/tutorials/tutorial_api_03#image-statistics). That was a *spatial* reduction. Reducing\nan image collection to an image is a *temporal* reduction when the collection\nrepresents images over time. The type of `Reducer` you use defines how Earth\nEngine handles overlapping pixels. Landsat 8 visits the same spot on the Earth every 16\ndays. That means that over a 6 month period, there will be approximately 12 images (and\nmore where the scenes overlap). Each pixel on the map is derived from a stack of pixels\n- one from each image in the collection being displayed.\n\nMerely adding the collection to the map results in selecting the most recent pixel - the\none from the latest image in the stack. This behavior may be altered, using Earth\nEngine reducers. For example, rather than take the most recent pixel from the stack,\nEarth Engine can be instructed to pick the median value in the stack. This has the\nbenefit of removing clouds (which have a high value) and shadows (which have a low\nvalue). When an image collection is reduced using the median reducer,\nthe composite value is the median in each band, over time. For example, using Landsat scenes\nin 2016:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Get the median over time, in each band, in each pixel.\nvar median = l8.filterDate('2016-01-01', '2016-12-31').median();\n\n// Make a handy variable of visualization parameters.\nvar visParams = {bands: ['B4', 'B3', 'B2'], max: 0.3};\n\n// Display the median composite.\nMap.addLayer(median, visParams, 'median');\n```\n\nThe new thing in this code is the `median()` method applied to an image collection.\nLike the filtering methods, this is a shortcut for the more general `reduce()`\nmethod on image collections which takes an `ee.Reducer()` as an argument. See\nthe `ee.Reducer` package in the **Docs** tab of the Code Editor\nto see a list of all the Earth Engine reducers. When considering a reducer for an\nimage collection, note that the output is an image, so reducers with non-numeric outputs,\nfor example `histogram` or `toList` reducers, won't work\nwith an image collection.\nFigure 6. Landsat 8 median composite.\n\nWhen you zoom out on the median composite, you should see something like Figure 6. This\nshould look considerably better than the recent value composite you made previously. At\nthis point, it's worth stepping back and considering what's been done to make that\nmedian composite. Earth Engine has loaded the entire Landsat 8 collection over the\ncontinental US, and has calculated the median for every pixel. That's a lot of data! Of\ncourse, you could compute annual medians, by first filtering the collection,\n[as you've done previously](/earth-engine/tutorials/tutorial_api_04#filtering-image-collections). The\npoint is that if you had to download all that imagery and make this composite, it would\nbe a big project. With Earth Engine, you get a result in seconds!\n\nLearn more about compositing and mosaicking [here](/earth-engine/guides/ic_composite_mosaic).\n\nMasking\n-------\n\nAlthough the median composite is an improvement over the recent-value composite, you\nmay want to mask parts of the image. Masking pixels in an image makes those pixels\ntransparent and excludes them from analysis. Each pixel in each band of an image has a\nmask. Those with a mask value of 0 or below will be transparent. Those with a mask of\nany value above 0 will be rendered. The mask of an image is set using a call like\n`image1.mask(image2)`. This call takes the values of `image2` and\nmakes them the mask of `image1`. Any pixels in `image2` that have\nthe value 0 will be made transparent in `image1`.\n\nFor example, suppose you would like to mask all the water pixels in the median\ncomposite. A water mask can be created using the dataset described by\n[Hansen et al. (2013)](http://www.sciencemag.org/content/342/6160/850) which is\nin the Earth Engine data catalog. (Learn more about the Hansen et al. dataset in\n[this tutorial](/earth-engine/tutorials/tutorial_forest_01).) In this dataset, water has a value of 2,\nland has the value 1, and 'no data' has the value 0. Use a bit of logic to create a mask\nimage that has zeros where there's no land:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Load or import the Hansen et al. forest change dataset.\nvar hansenImage = ee.Image('UMD/hansen/global_forest_change_2015');\n\n// Select the land/water mask.\nvar datamask = hansenImage.select('datamask');\n\n// Create a binary mask.\nvar mask = datamask.eq(1);\n\n// Update the composite mask with the water mask.\nvar maskedComposite = median.updateMask(mask);\nMap.addLayer(maskedComposite, visParams, 'masked');\n```\n\nThere are a couple new things in this code that are worth mentioning in detail. First,\nthe `select()` function is useful for extracting the bands of interest from\nan image. Here, we select only the band we care about: `datamask`. The next\nnew thing is the logical operator `eq()` which stands for \"equals.\" We use\n`eq(1)` to create a binary image in which all the pixels that do not have the\nvalue of 1 in the `datamask` band (those that are water or no data) get a value\nof 0 in the resulting image.\n\nAs a result of this masking, all the pixels in the median composite that are over land\n(according to the\n[Hansen\net al. dataset](/earth-engine/datasets/catalog/UMD_hansen_global_forest_change_2015_v1_3)) are visible, but those over water (or nodata) are transparent and\nwill be excluded from any analysis you do on the `maskedComposite` image.\n\nMosaicking\n----------\n\nBy combining the concepts of image collections, logical operators, masking and\ncompositing, you can achieve interesting cartographic results. For example, suppose\nyou want an image in which land pixels are displayed in true-color and all the other\npixels are displayed in blue, you can do something like:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Make a water image out of the mask.\nvar water = mask.not();\n\n// Mask water with itself to mask all the zeros (non-water).\nwater = water.mask(water);\n\n// Make an image collection of visualization images.\nvar mosaic = ee.ImageCollection([\n median.visualize(visParams),\n water.visualize({palette: '000044'}),\n]).mosaic();\n\n// Display the mosaic.\nMap.addLayer(mosaic, {}, 'custom mosaic');\n```\n\nThere's a lot going on in that code, so let's dissect it. First, we use the `not()`\nlogical operator to invert the mask we made earlier. Specifically, `not()`\nturns all the zeros into ones and all the non-zeros into zeros. It's not completely\ncorrect to call that variable `water` because it includes some nodata pixels as\nwell, but it's OK in the present cartographic context. The next thing is to mask the\n\"water\" with itself. This results in an image in which all the water pixels are 1's and\neverything else is masked. The final step is to combine the images with\n`mosaic()`. Since `mosaic()` works on an image collection, we pass a\nlist of images that we want to combine into the image collection constructor, then\ncall `mosaic()` as the final step. The order of the images in that list is\nimportant. Specifically, the output image will contain the last unmasked pixel from the\nstack of images in the input collection. In this case, that works because the water layer\nis the last (top) image in the collection, and only contains un-masked pixels where water\noccurs.\n\nNote that the images in the collection are\n[visualization images](/earth-engine/guides/image_visualization#visualization-images). When you call\n[`visualize()`](/earth-engine/apidocs/ee-image-visualize)\non an image, it gets turned into a 3-band, 8-bit image according to the visualization\nparameters you pass in. The default visualization parameters work fine for 3-band,\n8-bit images, so you don't need visualization parameters when you add the image to the\nmap. The result should look like Figure 7.\nFigure 7. Custom mosaic that makes water areas a uniform color.\n\nAt this point, you've seen ways to visualize image collections as recent-value composites,\nmethods for compositing image collections using reducers, and methods for making custom\ncomposites by masking and mosaicking a collection of images. In the next page, learn how to\nadd a vegetation index to every image in a collection and use the index to make a\n\"greenest pixel\" composite. \n[arrow_backPrevious page](/earth-engine/tutorials/tutorial_api_04) [Next pagearrow_forward](/earth-engine/tutorials/tutorial_api_06)"]]