The Wayback Machine - https://web.archive.org/web/20240918003308/https://www.geeksforgeeks.org/impact-of-image-flattening/
Open In App

Impact of Image Flattening

Last Updated : 03 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Flattening is a technique that is used to convert multi-dimensional arrays into a 1-D array, it is generally used in Deep Learning while feeding the 1-D array information to the classification model.

What is the need for Flattening of an Image?

Multi-Dimensional arrays take more amount of memory while 1-D arrays take less memory, which is the most important reason why we flatten the Image Array before processing/feeding the information to our model. In most cases, we will be dealing with a dataset which contains a large amount of images thus flattening helps in decreasing the memory as well as reducing the time to train the model.

Step 1: Importing the necessary libraries

Python
import numpy as np
import pandas as pd
import cv2 as cv
from google.colab.patches import cv2_imshow
from skimage import io
from PIL import Image
import matplotlib.pylab as plt
from numpy import array
from sys import getsizeof

Step 2: Fetching a random image through web

Python
#Fetching the url and showing the image using cv2_imshow
urls=["https://iiif.lib.ncsu.edu/iiif/0052574/full/800,/0/default.jpg"]
for url in urls:
  image = io.imread(url)
  cv2_imshow(image)
  print('\n')
Flattening1-copy


Step 3: Transforming the image into a multi-dimensional array

Python
#Getting the multi-dimensional array from the image
array1 = array(image)
#Memory occupied by the multi-dimensional array
size1 = getsizeof(array1)
print(array1)

Step 4: Now Flattening the multi-dimensional array using flatten() function

Python
#Using Flatten function on array 1 to convert the multi-dimensional 
# array to 1-D array
array2 = array1.flatten()
#Memory occupied by array 2
size2 = getsizeof(array2)
#displaying the 1-D array
print(array2)

Step5: Results of Flattening

Python
#Print's the two different size's of the array
print(f"Size of Multidimensional Image : {size1}")
print(f"Size of Flattened Image : {size2}")
difference = size1 - size2
#Print's the difference of memory between the size of Multidimensional & 1-D array
print("Size difference in the images: ", difference)
Size of Multidimensional Image : 1324928 
Size of Flattened Image : 1324896
Size difference in the images: 32

Step 6: Full Code

Python
#importing libraries
import numpy as np
import pandas as pd
import cv2 as cv
from google.colab.patches import cv2_imshow
from skimage import io
from PIL import Image
import matplotlib.pylab as plt
from numpy import array
from sys import getsizeof

#Fetching the url and showing the image using cv2_imshow
urls=["https://iiif.lib.ncsu.edu/iiif/0052574/full/800,/0/default.jpg"]
for url in urls:
  image = io.imread(url)
  cv2_imshow(image)
  print('\n')

#Getting the multi-dimensional array from the image
array1 = array(image)
#Memory occupied by the multi-dimensional array
size1 = getsizeof(array1)
print(array1)

#Using Flatten function on array 1 to convert the multi-dimensional 
# array to 1-D array
array2 = array1.flatten()
#Memory occupied by array 2
size2 = getsizeof(array2)
#displaying the 1-D array
print(array2)

#Print's the two different size's of the array
print(f"Size of Multidimensional Image : {size1}")
print(f"Size of Flattened Image : {size2}")
difference = size1 - size2
#Print's the difference of memory between the size of Multidimensional & 1-D array
print(difference)

Conclusion:

After running the whole code we see that there is not a major difference in memory used in the multi-dimensional image array and the flattened array. Then people may ask why we are doing the flattening when the effect is negligible. In a large datasets when we are dealing with thousands of images the net amount of the memory saved due to all the images accumulates to be pretty big.



Similar Reads

NLP | Flattening Deep Tree
Some of the corpora that we use are often deep trees of nested phrases. But working on such deep trees is a tedious job for training the chunker. As IOB tag parsing is not designed for nested chunks. So, in order to use these trees for chunker training, we must flatten them. Well, POS (part of Speech) are actually part of the tree structure instead
2 min read
Image Processing in Java - Colored Image to Grayscale Image Conversion
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and set PixelsIn this article, we will be converting a colored image to a grayscale image. RGB Color Model - The RGB color model is an additive mixing model in which red, green, and blue light are added together in various ways to reproduce a broad array of color
3 min read
Image Processing in Java - Colored image to Negative Image Conversion
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored image to Grayscale Image Conversion In this set, we will be converting a colored image to a negative image. Colored Image (RGB Color Model) - The RGB color model is an additive mixing model in which red, green, and
3 min read
Image Processing in Java - Colored Image to Sepia Image Conversion
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image Conversion In this set, we will be converting a col
3 min read
How Machine Learning and Artificial Intelligence Will Impact Global Industries in 2020?
Artificial Intelligence is quite powerful and is constantly evolving at an exponential rate. Artificial intelligence currently knows no bounds and is focused on outperforming its limits using the power of Machine Learning. AI is empowering computers to do things that human beings are unable to do efficiently and effectively and Machine Learning is
5 min read
Impact of AI and ML On Warfare Techniques
Artificial Intelligence provides the ability to adapt, reason and solutions to the problems. Modern warfare is becoming heavily reliant on Machine Learning and Artificial Intelligence. It is all because the integration of Al and ML enables machines to process a large amount of data with increased accuracy. Due to such enormous qualities, both Artif
5 min read
Impact and Example of Artificial Intelligence
Artificial Intelligence (AI) has been a game changer in recent years, transforming the way we live, work, and interact with the world. It is a kind of technology with the promise of transforming the way the world works. This is a never before seen scale of economic impact, it impacts every industry and aspect of our lives. Artificial Intelligence m
4 min read
How will AI Impact the Future of Work and Life?
In the last few years, the buzz about AI is sky-high! Will AI replace humans? Will AI create routes to more work possibilities? And thousands of similar questions! Well, artificial Intelligence might replace specific jobs in the market in the coming future, however, it has always created more jobs in all industries. There are several other reasons
9 min read
ChatGPT Impact on the Global Job Market
Over the past few years, artificial intelligence (AI) and automation in the workforce have given birth to the phobia of layoffs across industries. While emerging technologies offer multiple benefits, including increased efficiency, accuracy, and productivity, it has started to kill jobs that are becoming redundant in today’s age. The arrival of AI-
7 min read
Impact of learning rate on a model
In Machine Learning, there are two types of parameters: machine learnable parameters and hyper-parameters. Machine-learnable parameters are estimated by the algorithm during training, while hyper-parameters are set by the data scientist or ML engineer to regulate how the algorithm learns and modifies the model's performance. One such hyper-paramete
6 min read
What is the impact of AI on the future of data science?
Artificial Intelligence has an impact on the human world. In the era of digitization and modernization, it has developed in many fields including knowledge and creativity. It follows the core principle of integrating with the human world and helping them via text generation, decision-making, and research. In this article, we will explore about Impa
8 min read
Impact of Dataset Size on Deep Learning Model
In the field of deep learning, where models are designed to learn intricate patterns and representations from data, the significance of the dataset size cannot be overstated. The amount of data available for training has a profound impact on the performance, robustness, and generalization capabilities of deep learning models. In this article, we wi
9 min read
How Decision Tree Depth Impact on the Accuracy
Decision trees are a popular machine learning model due to its simplicity and interpretation. They work by recursively splitting the dataset into subsets based on the feature that provides the most information gain. One key parameter in decision tree models is the maximum depth of the tree, which determines how deep the tree can grow. In this artic
6 min read
The Impact of Data Science in the Energy Industry
Data Science has become a very useful tool to drive innovation as well as efficiency within the energy industry one of the most important significant impacts it has provided is its role in optimization of the energy consumption patterns through the advanced data analytics the energy companies can easily analyze large amounts of data to identify var
8 min read
The Impact Of Artificial Intelligence On The Art World
Artificial Intelligence (AI) has emerged as a transformative force across various industries, revolutionizing how we create, consume, and interact with information. In the realm of art, AI is not merely a tool but a catalyst for innovation, pushing the boundaries of creativity and challenging traditional practices. This article explores the profoun
6 min read
The Impact of Big Data on Business
Big data has become instrumental in the global business environment since it offers more information and productivity to support a company’s decision-making agenda. Analyzing large amounts of information at once obtained from different sources makes it possible to identify new opportunities and improve the quality of business services for clients.
11 min read
AI Influencers : The Rise, Impact, and Future of Virtual Personalities
In the ever-evolving landscape of social media and digital marketing, a new breed of influencers is emerging: AI influencers. These virtual personalities, entirely crafted by artificial intelligence, are revolutionizing the way brands engage with consumers. By blending advanced AI technology with creative design, AI influencers are not only captiva
5 min read
Role of Algorithmic Bias in AI: Understanding and Mitigating Its Impact
Artificial Intelligence (AI) is deeply embedded in modern technology, shaping many aspects of our daily lives. From healthcare to finance, AI systems are increasingly relied upon to make crucial decisions. However, as the use of AI expands, the problem of algorithmic bias has become a pressing issue. Algorithmic bias occurs when AI systems generate
7 min read
Exploring AI-Powered Coding Assistants and Their Impact on the Development Industry
The rise of artificial intelligence (AI) has revolutionized several industries, and the software development industry is no exception. Among the most significant advancements are AI-powered coding assistants, which are reshaping the way developers write, debug, and optimize code. These tools not only improve productivity but also enhance the overal
6 min read
Mahotas - Labelled Image from the Normal Image
In this article we will see how we can create a labelled image from the normal image in mahotas. For this we are going to use the fluorescent microscopy image from a nuclear segmentation benchmark. We can get the image with the help of command given below mahotas.demos.nuclear_image() Below is the nuclear_image Labelled images are integer images wh
2 min read
Image processing with Scikit-image in Python
scikit-image is an image processing Python package that works with NumPy arrays which is a collection of algorithms for image processing. Let's discuss how to deal with images in set of information and its application in the real world. Important features of scikit-image : Simple and efficient tools for image processing and computer vision techniqu
2 min read
MATLAB | RGB image to grayscale image conversion
An RGB image can be viewed as three images( a red scale image, a green scale image and a blue scale image) stacked on top of each other. In MATLAB, an RGB image is basically a M*N*3 array of colour pixel, where each colour pixel is a triplet which corresponds to red, blue and green colour component of RGB image at a specified spatial location. Simi
4 min read
MATLAB | Converting a Grayscale Image to Binary Image using Thresholding
Thresholding is the simplest method of image segmentation and the most common way to convert a grayscale image to a binary image.In thresholding, we select a threshold value and then all the gray level value which is below the selected threshold value is classified as 0(black i.e background ) and all the gray level which is equal to or greater than
3 min read
Image Edge Detection Operators in Digital Image Processing
Edges are significant local changes of intensity in a digital image. An edge can be defined as a set of connected pixels that forms a boundary between two disjoint regions. There are three types of edges: Horizontal edgesVertical edgesDiagonal edges Edge Detection is a method of segmenting an image into regions of discontinuity. It is a widely used
4 min read
Multi-Label Image Classification - Prediction of image labels
There are so many things we can do using computer vision algorithms: Object detectionImage segmentationImage translationObject tracking (in real-time), and a whole lot more. What is Multi-Label Image Classification? Let’s understand the concept of multi-label image classification with an intuitive example. If I show you an image of a ball, you'll e
8 min read
Image-to-Image Translation using Pix2Pix
Pix2pix GANs were proposed by researchers at UC Berkeley in 2017. It uses a conditional Generative Adversarial Network to perform the image-to-image translation task (i.e. converting one image to another, such as facades to buildings and Google Maps to Google Earth, etc. Architecture: The pix2pix uses conditional generative adversarial networks (co
9 min read
Convert Text Image to Hand Written Text Image using Python
In this article, we are going to see how to convert text images to handwritten text images using PyWhatkit, Pillow, and Tesseract in Python. Module needed: Pytesseract: Sometimes known as Python-tesseract, is a Python-based optical character recognition (OCR) program. It can read and recognize text in photos, license plates, and other documents. To
2 min read
Image Processing in Java - Colored to Red Green Blue Image Conversion
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image Conversion In this set, we will be converting a colored image to an image with either red effect, green effect, or blue
5 min read
Image Processing in Java - Creating a Random Pixel Image
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image ConversionImage Processing in Java - Colored Image
2 min read
Image Processing in Java - Creating a Mirror Image
Prerequisite: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image ConversionImage Processing in Java - Colored Image t
3 min read